Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Glue for Functions with mozilla::Range Parameters #434

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions mozjs-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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",
Expand Down
39 changes: 30 additions & 9 deletions mozjs-sys/src/jsglue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
#include "jsapi.h"
#include "jsfriendapi.h"


namespace glue {

// Reexport some functions that are marked inline.
Expand Down Expand Up @@ -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<JS::PropertyDescriptor> desc, JS::MutableHandle<JS::Value> vp);
bool JS_GetOwnPropertyDescriptorById(JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandle<JS::PropertyDescriptor> desc, bool* isNone);
bool JS_GetOwnPropertyDescriptor(JSContext* cx, JS::HandleObject obj, const char* name, JS::MutableHandle<JS::PropertyDescriptor> desc, bool* isNone);
bool JS_GetOwnUCPropertyDescriptor(JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen, JS::MutableHandle<JS::PropertyDescriptor> desc, bool* isNone);
bool JS_GetPropertyDescriptorById(JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandle<JS::PropertyDescriptor> desc, JS::MutableHandleObject holder, bool* isNone);
bool JS_GetPropertyDescriptor(JSContext* cx, JS::HandleObject obj, const char* name, JS::MutableHandle<JS::PropertyDescriptor> desc, JS::MutableHandleObject holder, bool* isNone);
bool JS_GetUCPropertyDescriptor(JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen, JS::MutableHandle<JS::PropertyDescriptor> desc, JS::MutableHandleObject holder, bool* isNone);
bool SetPropertyIgnoringNamedGetter(JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::HandleValue v, JS::HandleValue receiver, JS::Handle<JS::PropertyDescriptor> ownDesc, JS::ObjectOpResult& result);
bool FromPropertyDescriptor(JSContext* cx,
JS::Handle<JS::PropertyDescriptor> desc,
JS::MutableHandle<JS::Value> vp);
bool JS_GetOwnPropertyDescriptorById(
JSContext* cx, JS::HandleObject obj, JS::HandleId id,
JS::MutableHandle<JS::PropertyDescriptor> desc, bool* isNone);
bool JS_GetOwnPropertyDescriptor(JSContext* cx, JS::HandleObject obj,
const char* name,
JS::MutableHandle<JS::PropertyDescriptor> desc,
bool* isNone);
bool JS_GetOwnUCPropertyDescriptor(
JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen,
JS::MutableHandle<JS::PropertyDescriptor> desc, bool* isNone);
bool JS_GetPropertyDescriptorById(
JSContext* cx, JS::HandleObject obj, JS::HandleId id,
JS::MutableHandle<JS::PropertyDescriptor> desc,
JS::MutableHandleObject holder, bool* isNone);
bool JS_GetPropertyDescriptor(JSContext* cx, JS::HandleObject obj,
const char* name,
JS::MutableHandle<JS::PropertyDescriptor> desc,
JS::MutableHandleObject holder, bool* isNone);
bool JS_GetUCPropertyDescriptor(JSContext* cx, JS::HandleObject obj,
const char16_t* name, size_t namelen,
JS::MutableHandle<JS::PropertyDescriptor> desc,
JS::MutableHandleObject holder, bool* isNone);
bool SetPropertyIgnoringNamedGetter(JSContext* cx, JS::HandleObject obj,
JS::HandleId id, JS::HandleValue v,
JS::HandleValue receiver,
JS::Handle<JS::PropertyDescriptor> ownDesc,
JS::ObjectOpResult& result);

bool CreateError(JSContext* cx, JSExnType type, JS::HandleObject stack,
JS::HandleString fileName, uint32_t lineNumber,
Expand Down
2 changes: 1 addition & 1 deletion mozjs-sys/src/jsimpls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ impl JS::ForOfIterator {
}

impl<T> mozilla::Range<T> {
pub fn new(start: &mut T, end: &mut T) -> mozilla::Range<T> {
pub fn new(start: *mut T, end: *mut T) -> mozilla::Range<T> {
mozilla::Range {
mStart: mozilla::RangedPtr {
mPtr: start,
Expand Down
1 change: 1 addition & 0 deletions mozjs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ fn main() {
const BLACKLIST_TYPES: &'static [&'static str] = &[
"JS::.*",
"already_AddRefed",
"mozilla::Range",
// we don't want it null
"EncodedStringCallback",
];
Expand Down
97 changes: 59 additions & 38 deletions mozjs/src/jsglue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -169,7 +172,7 @@ struct ProxyTraps {

bool (*getOwnPropertyDescriptor)(
JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
JS::MutableHandle<JS::PropertyDescriptor> desc, bool *isNone);
JS::MutableHandle<JS::PropertyDescriptor> desc, bool* isNone);
bool (*defineProperty)(JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
JS::Handle<JS::PropertyDescriptor> desc,
JS::ObjectOpResult& result);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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); \
} \
Expand Down Expand Up @@ -380,11 +384,13 @@ class WrapperProxyHandler : public js::Wrapper {

virtual bool getOwnPropertyDescriptor(
JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc) const override {
JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc)
const override {
if (mTraps.getOwnPropertyDescriptor) {
JS::Rooted<JS::PropertyDescriptor> 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 {
Expand Down Expand Up @@ -450,7 +456,8 @@ class ForwardingProxyHandler : public js::BaseProxyHandler {

virtual bool getOwnPropertyDescriptor(
JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc) const override {
JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc)
const override {
JS::Rooted<JS::PropertyDescriptor> pd(cx);
bool isNone = true;
bool result = mTraps.getOwnPropertyDescriptor(cx, proxy, id, &pd, &isNone);
Expand Down Expand Up @@ -574,10 +581,10 @@ void* GetRustJSPrincipalsPrivate(JSPrincipals* principals) {

bool InvokeGetOwnPropertyDescriptor(
const void* handler, JSContext* cx, JS::HandleObject proxy, JS::HandleId id,
JS::MutableHandle<JS::PropertyDescriptor> desc, bool *isNone) {
JS::MutableHandle<JS::PropertyDescriptor> desc, bool* isNone) {
JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> mpd(cx);
bool result = static_cast<const ForwardingProxyHandler*>(handler)
->getOwnPropertyDescriptor(cx, proxy, id, &mpd);
->getOwnPropertyDescriptor(cx, proxy, id, &mpd);
*isNone = mpd.isNothing();
if (!*isNone) {
desc.set(*mpd);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -1094,31 +1103,43 @@ bool DescribeScriptedCaller(JSContext* cx, char* buffer, size_t buflen,
return true;
}

void SetDataPropertyDescriptor(
JS::MutableHandle<JS::PropertyDescriptor> desc,
JS::HandleValue value,
uint32_t attrs
) {
void SetDataPropertyDescriptor(JS::MutableHandle<JS::PropertyDescriptor> desc,
JS::HandleValue value, uint32_t attrs) {
desc.set(JS::PropertyDescriptor::Data(value, attrs));
}

void SetAccessorPropertyDescriptor(
JS::MutableHandle<JS::PropertyDescriptor> desc,
JS::HandleObject getter,
JS::HandleObject setter,
uint32_t attrs
) {
JS::MutableHandle<JS::PropertyDescriptor> 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<JS::Stencil>* stencil
) {
already_AddRefed<JS::Stencil> retval = JS::FinishOffThreadStencil(cx, token, storage);
void FinishOffThreadStencil(JSContext* cx, JS::OffThreadToken* token,
JS::InstantiationStorage* storage,
already_AddRefed<JS::Stencil>* stencil) {
already_AddRefed<JS::Stencil> retval =
JS::FinishOffThreadStencil(cx, token, storage);
*stencil = std::move(retval);
}

JS::BigInt* JS_StringToBigInt(
JSContext* cx, const mozilla::Range<const JS::Latin1Char>* chars) {
return JS::StringToBigInt(cx, *chars);
}

JS::BigInt* JS_StringToBigInt1(JSContext* cx,
const mozilla::Range<const char16_t>* chars) {
return JS::StringToBigInt(cx, *chars);
}

bool CopyStringChars(JSContext* cx, const mozilla::Range<char16_t>* dest,
JSString* str) {
return JS_CopyStringChars(cx, *dest, str);
}

JS::Latin1CharsZ LossyTwoByteCharsToNewLatin1CharsZ(
JSContext* cx, const mozilla::Range<const char16_t>* tbchars) {
return JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, *tbchars);
}

} // extern "C"
50 changes: 50 additions & 0 deletions mozjs/tests/range.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}