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 5 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
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
27 changes: 26 additions & 1 deletion 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 Down Expand Up @@ -555,6 +556,12 @@ bool ShouldMeasureObject(JSObject* obj, nsISupports** iface) {
return false;
}

typedef mozilla::Range<const JS::Latin1Char> RangeLatin1;

typedef mozilla::Range<const char16_t> RangeConstUtf16;

typedef mozilla::Range<char16_t> RangeUtf16;

Redfire75369 marked this conversation as resolved.
Show resolved Hide resolved
extern "C" {

JSPrincipals* CreateRustJSPrincipals(const JSPrincipalsCallbacks& callbacks,
Expand Down Expand Up @@ -1121,4 +1128,22 @@ void FinishOffThreadStencil(
*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);
}
}