Skip to content

Commit ac557db

Browse files
committed
Added Glue for mozilla::Range Parameters
1 parent b662b3a commit ac557db

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

mozjs-sys/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,10 @@ const BLACKLIST_FUNCTIONS: &'static [&'static str] = &[
452452
"JS::GetExceptionCause",
453453
"JS::GetOptimizedEncodingBuildId",
454454
"JS::GetScriptTranscodingBuildId",
455+
"JS::LossyTwoByteCharsToNewLatin1CharsZ",
456+
"JS::StringToBigInt",
455457
"JS::dbg::FireOnGarbageCollectionHook",
458+
"JS_CopyStringChars",
456459
"JS_EncodeStringToUTF8BufferPartial",
457460
"JS_GetErrorType",
458461
"JS_GetOwnPropertyDescriptorById",

mozjs/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ fn main() {
120120
const BLACKLIST_TYPES: &'static [&'static str] = &[
121121
"JS::.*",
122122
"already_AddRefed",
123+
"mozilla::Range",
123124
// we don't want it null
124125
"EncodedStringCallback",
125126
];

mozjs/src/jsglue.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
#endif
1616

1717
#include "assert.h"
18+
#include "js/BigInt.h" // JS::StringToBigInt
1819
#include "js/BuildId.h"
1920
#include "js/Class.h"
2021
#include "js/Id.h"
2122
#include "js/MemoryMetrics.h"
22-
#include "js/Modules.h" // include for JS::GetModulePrivate
23+
#include "js/Modules.h" // JS::GetModulePrivate
2324
#include "js/Principals.h"
2425
#include "js/Promise.h"
2526
#include "js/Proxy.h"
@@ -555,6 +556,12 @@ bool ShouldMeasureObject(JSObject* obj, nsISupports** iface) {
555556
return false;
556557
}
557558

559+
typedef mozilla::Range<const JS::Latin1Char> RangeLatin1;
560+
561+
typedef mozilla::Range<const char16_t> RangeConstUtf16;
562+
563+
typedef mozilla::Range<char16_t> RangeUtf16;
564+
558565
extern "C" {
559566

560567
JSPrincipals* CreateRustJSPrincipals(const JSPrincipalsCallbacks& callbacks,
@@ -1121,4 +1128,22 @@ void FinishOffThreadStencil(
11211128
*stencil = std::move(retval);
11221129
}
11231130

1131+
JS::BigInt* JS_StringToBigInt(JSContext* cx, mozilla::Range<const JS::Latin1Char> chars) {
1132+
return JS::StringToBigInt(cx, chars);
1133+
}
1134+
1135+
JS::BigInt* JS_StringToBigInt1(JSContext* cx, mozilla::Range<const char16_t> chars) {
1136+
return JS::StringToBigInt(cx, chars);
1137+
}
1138+
1139+
bool CopyStringChars(JSContext* cx, mozilla::Range<char16_t> dest,
1140+
JSString* str) {
1141+
return JS_CopyStringChars(cx, dest, str);
1142+
}
1143+
1144+
JS::Latin1CharsZ LossyTwoByteCharsToNewLatin1CharsZ(
1145+
JSContext* cx, const mozilla::Range<const char16_t> tbchars) {
1146+
return JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, tbchars);
1147+
}
1148+
11241149
} // extern "C"

mozjs/tests/range.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::ptr;
22

33
use mozjs::jsapi::mozilla::Range;
4-
use mozjs::jsapi::{BigIntIsUint64, JS_NewGlobalObject, StringToBigInt, StringToBigInt1};
4+
use mozjs::jsapi::{BigIntIsUint64, JS_NewGlobalObject};
55
use mozjs::jsapi::{JSAutoRealm, OnNewGlobalHookOption};
6+
use mozjs::glue::{JS_StringToBigInt, JS_StringToBigInt1};
67
use mozjs::rooted;
78
use mozjs::rust::{JSEngine, RealmOptions, Runtime, SIMPLE_GLOBAL_CLASS};
89

@@ -29,7 +30,7 @@ fn range() {
2930
let mut string = int.to_string();
3031
let range = string.as_bytes_mut().as_mut_ptr_range();
3132
let chars = Range::new(range.start, range.end);
32-
rooted!(in(context) let bigint = StringToBigInt(context, chars));
33+
rooted!(in(context) let bigint = JS_StringToBigInt(context, chars));
3334
assert!(!bigint.get().is_null());
3435

3536
let mut result = 0;
@@ -39,7 +40,7 @@ fn range() {
3940
let mut chars: Vec<_> = string.encode_utf16().collect();
4041
let range = chars.as_mut_ptr_range();
4142
let chars = Range::new(range.start, range.end);
42-
rooted!(in(context) let bigint = StringToBigInt1(context, chars));
43+
rooted!(in(context) let bigint = JS_StringToBigInt1(context, chars));
4344
assert!(!bigint.get().is_null());
4445

4546
let mut result = 0;

0 commit comments

Comments
 (0)