diff --git a/mozjs-sys/build.rs b/mozjs-sys/build.rs index c3a7196431e..65f93925cd2 100644 --- a/mozjs-sys/build.rs +++ b/mozjs-sys/build.rs @@ -28,12 +28,8 @@ const ENV_VARS: &'static [&'static str] = &[ "STLPORT_LIBS", ]; -const EXTRA_FILES: &'static [&'static str] = &[ - "makefile.cargo", - "src/rustfmt.toml", - "src/jsglue.hpp", - "src/jsglue.cpp", -]; +const EXTRA_FILES: &'static [&'static str] = + &["makefile.cargo", "src/jsglue.hpp", "src/jsglue.cpp"]; /// Which version of moztools we expect #[cfg(windows)] @@ -458,8 +454,11 @@ const BLACKLIST_FUNCTIONS: &'static [&'static str] = &[ "JS::GetScriptPrivate", "JS::GetScriptTranscodingBuildId", "JS::GetScriptedCallerPrivate", + "JS::LossyTwoByteCharsToNewLatin1CharsZ", "JS::MaybeGetScriptPrivate", + "JS::StringToBigInt", "JS::dbg::FireOnGarbageCollectionHook", + "JS_CopyStringChars", "JS_EncodeStringToUTF8BufferPartial", "JS_GetEmptyStringValue", "JS_GetErrorType", diff --git a/mozjs-sys/src/jsglue.hpp b/mozjs-sys/src/jsglue.hpp index ba3b882621e..b2abb6b23f2 100644 --- a/mozjs-sys/src/jsglue.hpp +++ b/mozjs-sys/src/jsglue.hpp @@ -48,7 +48,6 @@ #include "jsapi.h" #include "jsfriendapi.h" - namespace glue { // Reexport some functions that are marked inline. @@ -108,14 +107,36 @@ bool JS_ValueIsUndefined(const JS::Value* value); // These types are using maybe so we manually unwrap them in these wrappers -bool FromPropertyDescriptor(JSContext *cx, JS::Handle desc, JS::MutableHandle vp); -bool JS_GetOwnPropertyDescriptorById(JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandle desc, bool* isNone); -bool JS_GetOwnPropertyDescriptor(JSContext* cx, JS::HandleObject obj, const char* name, JS::MutableHandle desc, bool* isNone); -bool JS_GetOwnUCPropertyDescriptor(JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen, JS::MutableHandle desc, bool* isNone); -bool JS_GetPropertyDescriptorById(JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandle desc, JS::MutableHandleObject holder, bool* isNone); -bool JS_GetPropertyDescriptor(JSContext* cx, JS::HandleObject obj, const char* name, JS::MutableHandle desc, JS::MutableHandleObject holder, bool* isNone); -bool JS_GetUCPropertyDescriptor(JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen, JS::MutableHandle desc, JS::MutableHandleObject holder, bool* isNone); -bool SetPropertyIgnoringNamedGetter(JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::HandleValue v, JS::HandleValue receiver, JS::Handle ownDesc, JS::ObjectOpResult& result); +bool FromPropertyDescriptor(JSContext* cx, + JS::Handle desc, + JS::MutableHandle vp); +bool JS_GetOwnPropertyDescriptorById( + JSContext* cx, JS::HandleObject obj, JS::HandleId id, + JS::MutableHandle desc, bool* isNone); +bool JS_GetOwnPropertyDescriptor(JSContext* cx, JS::HandleObject obj, + const char* name, + JS::MutableHandle desc, + bool* isNone); +bool JS_GetOwnUCPropertyDescriptor( + JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen, + JS::MutableHandle desc, bool* isNone); +bool JS_GetPropertyDescriptorById( + JSContext* cx, JS::HandleObject obj, JS::HandleId id, + JS::MutableHandle desc, + JS::MutableHandleObject holder, bool* isNone); +bool JS_GetPropertyDescriptor(JSContext* cx, JS::HandleObject obj, + const char* name, + JS::MutableHandle desc, + JS::MutableHandleObject holder, bool* isNone); +bool JS_GetUCPropertyDescriptor(JSContext* cx, JS::HandleObject obj, + const char16_t* name, size_t namelen, + JS::MutableHandle desc, + JS::MutableHandleObject holder, bool* isNone); +bool SetPropertyIgnoringNamedGetter(JSContext* cx, JS::HandleObject obj, + JS::HandleId id, JS::HandleValue v, + JS::HandleValue receiver, + JS::Handle ownDesc, + JS::ObjectOpResult& result); bool CreateError(JSContext* cx, JSExnType type, JS::HandleObject stack, JS::HandleString fileName, uint32_t lineNumber, diff --git a/mozjs-sys/src/jsimpls.rs b/mozjs-sys/src/jsimpls.rs index 4f74d6ca4ca..2b63737090f 100644 --- a/mozjs-sys/src/jsimpls.rs +++ b/mozjs-sys/src/jsimpls.rs @@ -547,7 +547,7 @@ impl JS::ForOfIterator { } impl mozilla::Range { - pub fn new(start: &mut T, end: &mut T) -> mozilla::Range { + pub fn new(start: *mut T, end: *mut T) -> mozilla::Range { mozilla::Range { mStart: mozilla::RangedPtr { mPtr: start, diff --git a/mozjs/build.rs b/mozjs/build.rs index ccf2cbd4373..3969b430274 100644 --- a/mozjs/build.rs +++ b/mozjs/build.rs @@ -120,6 +120,7 @@ fn main() { const BLACKLIST_TYPES: &'static [&'static str] = &[ "JS::.*", "already_AddRefed", + "mozilla::Range", // we don't want it null "EncodedStringCallback", ]; diff --git a/mozjs/src/jsglue.cpp b/mozjs/src/jsglue.cpp index 2d23b0a34a1..39bdeec4ca2 100644 --- a/mozjs/src/jsglue.cpp +++ b/mozjs/src/jsglue.cpp @@ -15,11 +15,12 @@ #endif #include "assert.h" +#include "js/BigInt.h" // JS::StringToBigInt #include "js/BuildId.h" #include "js/Class.h" #include "js/Id.h" #include "js/MemoryMetrics.h" -#include "js/Modules.h" // include for JS::GetModulePrivate +#include "js/Modules.h" // JS::GetModulePrivate #include "js/Principals.h" #include "js/Promise.h" #include "js/Proxy.h" @@ -28,8 +29,8 @@ #include "js/Stream.h" #include "js/StructuredClone.h" #include "js/Wrapper.h" -#include "js/experimental/JitInfo.h" #include "js/experimental/JSStencil.h" +#include "js/experimental/JitInfo.h" #include "js/experimental/TypedData.h" #include "js/friend/ErrorMessages.h" #include "jsapi.h" @@ -84,11 +85,12 @@ class RustJobQueue : public JS::JobQueue { }; struct ReadableStreamUnderlyingSourceTraps { - void (*requestData)(const void* source, JSContext* cx, JS::HandleObject stream, - size_t desiredSize); + void (*requestData)(const void* source, JSContext* cx, + JS::HandleObject stream, size_t desiredSize); void (*writeIntoReadRequestBuffer)(const void* source, JSContext* cx, - JS::HandleObject stream, JS::HandleObject chunk, - size_t length, size_t* bytesWritten); + JS::HandleObject stream, + JS::HandleObject chunk, size_t length, + size_t* bytesWritten); void (*cancel)(const void* source, JSContext* cx, JS::HandleObject stream, JS::HandleValue reason, JS::Value* resolve_to); void (*onClosed)(const void* source, JSContext* cx, JS::HandleObject stream); @@ -113,10 +115,11 @@ class RustReadableStreamUnderlyingSource } virtual void writeIntoReadRequestBuffer(JSContext* cx, - JS::HandleObject stream, JS::HandleObject chunk, - size_t length, size_t* bytesWritten) { - return mTraps.writeIntoReadRequestBuffer(mSource, cx, stream, chunk, - length, bytesWritten); + JS::HandleObject stream, + JS::HandleObject chunk, size_t length, + size_t* bytesWritten) { + return mTraps.writeIntoReadRequestBuffer(mSource, cx, stream, chunk, length, + bytesWritten); } virtual JS::Value cancel(JSContext* cx, JS::HandleObject stream, @@ -169,7 +172,7 @@ struct ProxyTraps { bool (*getOwnPropertyDescriptor)( JSContext* cx, JS::HandleObject proxy, JS::HandleId id, - JS::MutableHandle desc, bool *isNone); + JS::MutableHandle desc, bool* isNone); bool (*defineProperty)(JSContext* cx, JS::HandleObject proxy, JS::HandleId id, JS::Handle desc, JS::ObjectOpResult& result); @@ -225,7 +228,7 @@ struct ProxyTraps { bool (*defaultValue)(JSContext* cx, JS::HandleObject obj, JSType hint, JS::MutableHandleValue vp); void (*trace)(JSTracer* trc, JSObject* proxy); - void (*finalize)(JS::GCContext *cx, JSObject* proxy); + void (*finalize)(JS::GCContext* cx, JSObject* proxy); size_t (*objectMoved)(JSObject* proxy, JSObject* old); bool (*isCallable)(JSObject* obj); @@ -324,7 +327,8 @@ static int HandlerFamily; mTraps.trace ? mTraps.trace(trc, proxy) : _base::trace(trc, proxy); \ } \ \ - virtual void finalize(JS::GCContext* context, JSObject* proxy) const override { \ + virtual void finalize(JS::GCContext* context, JSObject* proxy) \ + const override { \ mTraps.finalize ? mTraps.finalize(context, proxy) \ : _base::finalize(context, proxy); \ } \ @@ -380,11 +384,13 @@ class WrapperProxyHandler : public js::Wrapper { virtual bool getOwnPropertyDescriptor( JSContext* cx, JS::HandleObject proxy, JS::HandleId id, - JS::MutableHandle> desc) const override { + JS::MutableHandle> desc) + const override { if (mTraps.getOwnPropertyDescriptor) { JS::Rooted pd(cx); bool isNone = true; - bool result = mTraps.getOwnPropertyDescriptor(cx, proxy, id, &pd, &isNone); + bool result = + mTraps.getOwnPropertyDescriptor(cx, proxy, id, &pd, &isNone); if (isNone) { desc.set(mozilla::Nothing()); } else { @@ -450,7 +456,8 @@ class ForwardingProxyHandler : public js::BaseProxyHandler { virtual bool getOwnPropertyDescriptor( JSContext* cx, JS::HandleObject proxy, JS::HandleId id, - JS::MutableHandle> desc) const override { + JS::MutableHandle> desc) + const override { JS::Rooted pd(cx); bool isNone = true; bool result = mTraps.getOwnPropertyDescriptor(cx, proxy, id, &pd, &isNone); @@ -574,10 +581,10 @@ void* GetRustJSPrincipalsPrivate(JSPrincipals* principals) { bool InvokeGetOwnPropertyDescriptor( const void* handler, JSContext* cx, JS::HandleObject proxy, JS::HandleId id, - JS::MutableHandle desc, bool *isNone) { + JS::MutableHandle desc, bool* isNone) { JS::Rooted> mpd(cx); bool result = static_cast(handler) - ->getOwnPropertyDescriptor(cx, proxy, id, &mpd); + ->getOwnPropertyDescriptor(cx, proxy, id, &mpd); *isNone = mpd.isNothing(); if (!*isNone) { desc.set(*mpd); @@ -780,7 +787,8 @@ JSObject* UnwrapObjectStatic(JSObject* obj) { return js::CheckedUnwrapStatic(obj); } -JSObject* UnwrapObjectDynamic(JSObject* obj, JSContext* cx, bool stopAtWindowProxy) { +JSObject* UnwrapObjectDynamic(JSObject* obj, JSContext* cx, + bool stopAtWindowProxy) { return js::CheckedUnwrapDynamic(obj, cx, stopAtWindowProxy); } @@ -980,7 +988,7 @@ bool WriteBytesToJSStructuredCloneData(const uint8_t* src, size_t len, // MSVC uses a different calling convention for functions // that return non-POD values. Unfortunately, this includes anything -// with a constructor, such as JS::Value and JS::RegExpFlags, so we +// with a constructor, such as JS::Value and JS::RegExpFlags, so we // can't call these from Rust. These wrapper functions are only here // to ensure the calling convention is right. // https://web.archive.org/web/20180929193700/https://mozilla.logbot.info/jsapi/20180622#c14918658 @@ -1020,7 +1028,8 @@ void JS_GetReservedSlot(JSObject* obj, uint32_t index, JS::Value* dest) { *dest = JS::GetReservedSlot(obj, index); } -void JS_GetRegExpFlags(JSContext* cx, JS::HandleObject obj, JS::RegExpFlags* flags) { +void JS_GetRegExpFlags(JSContext* cx, JS::HandleObject obj, + JS::RegExpFlags* flags) { *flags = JS::GetRegExpFlags(cx, obj); } @@ -1094,31 +1103,43 @@ bool DescribeScriptedCaller(JSContext* cx, char* buffer, size_t buflen, return true; } -void SetDataPropertyDescriptor( - JS::MutableHandle desc, - JS::HandleValue value, - uint32_t attrs -) { +void SetDataPropertyDescriptor(JS::MutableHandle desc, + JS::HandleValue value, uint32_t attrs) { desc.set(JS::PropertyDescriptor::Data(value, attrs)); } void SetAccessorPropertyDescriptor( - JS::MutableHandle desc, - JS::HandleObject getter, - JS::HandleObject setter, - uint32_t attrs -) { + JS::MutableHandle desc, JS::HandleObject getter, + JS::HandleObject setter, uint32_t attrs) { desc.set(JS::PropertyDescriptor::Accessor(getter, setter, attrs)); } -void FinishOffThreadStencil( - JSContext* cx, - JS::OffThreadToken* token, - JS::InstantiationStorage* storage, - already_AddRefed* stencil -) { - already_AddRefed retval = JS::FinishOffThreadStencil(cx, token, storage); +void FinishOffThreadStencil(JSContext* cx, JS::OffThreadToken* token, + JS::InstantiationStorage* storage, + already_AddRefed* stencil) { + already_AddRefed retval = + JS::FinishOffThreadStencil(cx, token, storage); *stencil = std::move(retval); } +JS::BigInt* JS_StringToBigInt( + JSContext* cx, const mozilla::Range* chars) { + return JS::StringToBigInt(cx, *chars); +} + +JS::BigInt* JS_StringToBigInt1(JSContext* cx, + const mozilla::Range* chars) { + return JS::StringToBigInt(cx, *chars); +} + +bool CopyStringChars(JSContext* cx, const mozilla::Range* dest, + JSString* str) { + return JS_CopyStringChars(cx, *dest, str); +} + +JS::Latin1CharsZ LossyTwoByteCharsToNewLatin1CharsZ( + JSContext* cx, const mozilla::Range* tbchars) { + return JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, *tbchars); +} + } // extern "C" diff --git a/mozjs/tests/range.rs b/mozjs/tests/range.rs new file mode 100644 index 00000000000..a6407e545e9 --- /dev/null +++ b/mozjs/tests/range.rs @@ -0,0 +1,50 @@ +use std::ptr; + +use mozjs::glue::{JS_StringToBigInt, JS_StringToBigInt1}; +use mozjs::jsapi::mozilla::Range; +use mozjs::jsapi::{BigIntIsUint64, JS_NewGlobalObject}; +use mozjs::jsapi::{JSAutoRealm, OnNewGlobalHookOption}; +use mozjs::rooted; +use mozjs::rust::{JSEngine, RealmOptions, Runtime, SIMPLE_GLOBAL_CLASS}; + +#[test] +fn range() { + let engine = JSEngine::init().unwrap(); + let runtime = Runtime::new(engine.handle()); + let context = runtime.cx(); + let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook; + let c_option = RealmOptions::default(); + + unsafe { + rooted!(in(context) let global = JS_NewGlobalObject( + context, + &SIMPLE_GLOBAL_CLASS, + ptr::null_mut(), + h_option, + &*c_option, + )); + let _ac = JSAutoRealm::new(context, global.get()); + + // Number.MAX_SAFE_INTEGER + 10 + let int = 9007199254741001; + let mut string = int.to_string(); + let range = string.as_bytes_mut().as_mut_ptr_range(); + let chars = Range::new(range.start, range.end); + rooted!(in(context) let bigint = JS_StringToBigInt(context, &chars)); + assert!(!bigint.get().is_null()); + + let mut result = 0; + assert!(BigIntIsUint64(bigint.get(), &mut result)); + assert_eq!(result, int); + + let mut chars: Vec<_> = string.encode_utf16().collect(); + let range = chars.as_mut_ptr_range(); + let chars = Range::new(range.start, range.end); + rooted!(in(context) let bigint = JS_StringToBigInt1(context, &chars)); + assert!(!bigint.get().is_null()); + + let mut result = 0; + assert!(BigIntIsUint64(bigint.get(), &mut result)); + assert_eq!(result, int); + } +}