Skip to content

Commit

Permalink
Added Glue for mozilla::Range Parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Redfire75369 committed Dec 8, 2023
1 parent b662b3a commit ac557db
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
3 changes: 3 additions & 0 deletions mozjs-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,10 @@ const BLACKLIST_FUNCTIONS: &'static [&'static str] = &[
"JS::GetExceptionCause",
"JS::GetOptimizedEncodingBuildId",
"JS::GetScriptTranscodingBuildId",
"JS::LossyTwoByteCharsToNewLatin1CharsZ",
"JS::StringToBigInt",
"JS::dbg::FireOnGarbageCollectionHook",
"JS_CopyStringChars",
"JS_EncodeStringToUTF8BufferPartial",
"JS_GetErrorType",
"JS_GetOwnPropertyDescriptorById",
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;

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, mozilla::Range<const JS::Latin1Char> chars) {
return JS::StringToBigInt(cx, chars);
}

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

bool CopyStringChars(JSContext* cx, 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"
7 changes: 4 additions & 3 deletions mozjs/tests/range.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::ptr;

use mozjs::jsapi::mozilla::Range;
use mozjs::jsapi::{BigIntIsUint64, JS_NewGlobalObject, StringToBigInt, StringToBigInt1};
use mozjs::jsapi::{BigIntIsUint64, JS_NewGlobalObject};
use mozjs::jsapi::{JSAutoRealm, OnNewGlobalHookOption};
use mozjs::glue::{JS_StringToBigInt, JS_StringToBigInt1};
use mozjs::rooted;
use mozjs::rust::{JSEngine, RealmOptions, Runtime, SIMPLE_GLOBAL_CLASS};

Expand All @@ -29,7 +30,7 @@ fn range() {
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 = StringToBigInt(context, chars));
rooted!(in(context) let bigint = JS_StringToBigInt(context, chars));
assert!(!bigint.get().is_null());

let mut result = 0;
Expand All @@ -39,7 +40,7 @@ fn range() {
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 = StringToBigInt1(context, chars));
rooted!(in(context) let bigint = JS_StringToBigInt1(context, chars));
assert!(!bigint.get().is_null());

let mut result = 0;
Expand Down

0 comments on commit ac557db

Please sign in to comment.