Skip to content
This repository was archived by the owner on Nov 12, 2022. It is now read-only.

Commit 6893940

Browse files
committed
Added Wrapper for JSExternalStringCallbacks
1 parent e35fdec commit 6893940

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

src/glue_wrappers.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ wrap!(glue: pub fn RUST_SYMBOL_TO_JSID(sym: *mut Symbol, id: MutableHandleId));
1515
wrap!(glue: pub fn RUST_JSID_IS_VOID(id: HandleId) -> bool);
1616
wrap!(glue: pub fn RUST_INTERNED_STRING_TO_JSID(cx: *mut JSContext, str: *mut JSString, id: MutableHandleId));
1717
wrap!(glue: pub fn AppendToIdVector(v: MutableHandleIdVector, id: HandleId) -> bool);
18-
wrap!(glue: pub fn JS_GetPromiseResult (promise: HandleObject, dest: MutableHandleValue));
18+
wrap!(glue: pub fn JS_GetPromiseResult(promise: HandleObject, dest: MutableHandleValue));
1919
wrap!(glue: pub fn JS_GetScriptPrivate(script: *mut JSScript, dest: MutableHandleValue));
2020
wrap!(glue: pub fn JS_GetModulePrivate(module: *mut JSObject, dest: MutableHandleValue));
2121
wrap!(glue: pub fn EncodeStringToUTF8(cx: *mut JSContext, str: HandleString, cb: fn(*const c_char)));

src/jsapi_wrappers.in

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ wrap!(jsapi: pub fn ReadableStreamReaderReleaseLock(cx: *mut JSContext, reader:
174174
wrap!(jsapi: pub fn ReadableStreamDefaultReaderRead(cx: *mut JSContext, reader: HandleObject) -> *mut JSObject);
175175
wrap!(jsapi: pub fn IsWasmModuleObject(obj: HandleObject) -> bool);
176176
wrap!(jsapi: pub fn GetWasmModule(obj: HandleObject) -> RefPtr<WasmModule>);
177-
wrap!(jsapi: pub fn JS_EncodeStringToUTF8(cx: *mut JSContext, str: Handle<*mut JSString>) -> UniqueChars);
177+
wrap!(jsapi: pub fn JS_EncodeStringToUTF8(cx: *mut JSContext, str_: Handle<*mut JSString>) -> UniqueChars);
178178
wrap!(jsapi: pub fn JS_ValueToObject(cx: *mut JSContext, v: HandleValue, objp: MutableHandleObject) -> bool);
179179
wrap!(jsapi: pub fn JS_ValueToFunction(cx: *mut JSContext, v: HandleValue) -> *mut JSFunction);
180180
wrap!(jsapi: pub fn JS_ValueToConstructor(cx: *mut JSContext, v: HandleValue) -> *mut JSFunction);
@@ -301,15 +301,15 @@ wrap!(jsapi: pub fn JS_DefineFunctionById(cx: *mut JSContext, obj: HandleObject,
301301
wrap!(jsapi: pub fn JS_GetFunctionScript(cx: *mut JSContext, fun: HandleFunction) -> *mut JSScript);
302302
wrap!(jsapi: pub fn JS_DecompileScript(cx: *mut JSContext, script: Handle<*mut JSScript>) -> *mut JSString);
303303
wrap!(jsapi: pub fn JS_DecompileFunction(cx: *mut JSContext, fun: Handle<*mut JSFunction>) -> *mut JSString);
304-
wrap!(jsapi: pub fn JS_AtomizeAndPinJSString(cx: *mut JSContext, str: HandleString) -> *mut JSString);
305-
wrap!(jsapi: pub fn JS_NewDependentString(cx: *mut JSContext, str: HandleString, start: usize, length: usize) -> *mut JSString);
304+
wrap!(jsapi: pub fn JS_AtomizeAndPinJSString(cx: *mut JSContext, str_: HandleString) -> *mut JSString);
305+
wrap!(jsapi: pub fn JS_NewDependentString(cx: *mut JSContext, str_: HandleString, start: usize, length: usize) -> *mut JSString);
306306
wrap!(jsapi: pub fn JS_ConcatStrings(cx: *mut JSContext, left: HandleString, right: HandleString) -> *mut JSString);
307307
wrap!(jsapi: pub fn JS_GetPendingException(cx: *mut JSContext, vp: MutableHandleValue) -> bool);
308308
wrap!(jsapi: pub fn JS_SetPendingException(cx: *mut JSContext, v: HandleValue, behavior: ExceptionStackBehavior));
309309
wrap!(jsapi: pub fn JS_ErrorFromException(cx: *mut JSContext, obj: HandleObject) -> *mut JSErrorReport);
310310
wrap!(jsapi: pub fn JS_IndexToId(cx: *mut JSContext, index: u32, arg1: MutableHandleId) -> bool);
311311
wrap!(jsapi: pub fn JS_CharsToId(cx: *mut JSContext, chars: TwoByteChars, arg1: MutableHandleId) -> bool);
312-
wrap!(jsapi: pub fn JS_IsIdentifier(cx: *mut JSContext, str: HandleString, isIdentifier: *mut bool) -> bool);
312+
wrap!(jsapi: pub fn JS_IsIdentifier(cx: *mut JSContext, str_: HandleString, isIdentifier: *mut bool) -> bool);
313313
wrap!(jsapi: pub fn JS_FindCompilationScope(cx: *mut JSContext, obj: HandleObject) -> *mut JSObject);
314314
wrap!(jsapi: pub fn JS_NewObjectWithoutMetadata(cx: *mut JSContext, clasp: *const JSClass, proto: HandleObject) -> *mut JSObject);
315315
wrap!(jsapi: pub fn JS_NondeterministicGetWeakMapKeys(cx: *mut JSContext, obj: HandleObject, ret: MutableHandleObject) -> bool);
@@ -326,14 +326,14 @@ wrap!(jsapi: pub fn JS_ExecuteScript2(cx: *mut JSContext, envChain: HandleObject
326326
wrap!(jsapi: pub fn JS_ExecuteScript3(cx: *mut JSContext, envChain: HandleObjectVector, script: Handle<*mut JSScript>) -> bool);
327327
wrap!(jsapi: pub fn JS_Stringify(cx: *mut JSContext, value: MutableHandle<Value>, replacer: HandleObject, space: Handle<Value>, callback: JSONWriteCallback, data: *mut ::std::os::raw::c_void) -> bool);
328328
wrap!(jsapi: pub fn JS_ParseJSON(cx: *mut JSContext, chars: *const u16, len: u32, vp: MutableHandle<Value>) -> bool);
329-
wrap!(jsapi: pub fn JS_ParseJSON1(cx: *mut JSContext, str: Handle<*mut JSString>, vp: MutableHandle<Value>) -> bool);
329+
wrap!(jsapi: pub fn JS_ParseJSON1(cx: *mut JSContext, str_: Handle<*mut JSString>, vp: MutableHandle<Value>) -> bool);
330330
wrap!(jsapi: pub fn JS_ParseJSONWithReviver(cx: *mut JSContext, chars: *const u16, len: u32, reviver: Handle<Value>, vp: MutableHandle<Value>) -> bool);
331-
wrap!(jsapi: pub fn JS_ParseJSONWithReviver1(cx: *mut JSContext, str: Handle<*mut JSString>, reviver: Handle<Value>, vp: MutableHandle<Value>) -> bool);
331+
wrap!(jsapi: pub fn JS_ParseJSONWithReviver1(cx: *mut JSContext, str_: Handle<*mut JSString>, reviver: Handle<Value>, vp: MutableHandle<Value>) -> bool);
332332
wrap!(jsapi: pub fn JS_ReadStructuredClone(cx: *mut JSContext, data: *const JSStructuredCloneData, version: u32, scope: StructuredCloneScope, vp: MutableHandleValue, cloneDataPolicy: *const CloneDataPolicy, optionalCallbacks: *const JSStructuredCloneCallbacks, closure: *mut ::std::os::raw::c_void) -> bool);
333333
wrap!(jsapi: pub fn JS_WriteStructuredClone(cx: *mut JSContext, v: HandleValue, data: *mut JSStructuredCloneData, scope: StructuredCloneScope, cloneDataPolicy: *const CloneDataPolicy, optionalCallbacks: *const JSStructuredCloneCallbacks, closure: *mut ::std::os::raw::c_void, transferable: HandleValue) -> bool);
334334
wrap!(jsapi: pub fn JS_StructuredClone(cx: *mut JSContext, v: HandleValue, vp: MutableHandleValue, optionalCallbacks: *const JSStructuredCloneCallbacks, closure: *mut ::std::os::raw::c_void) -> bool);
335335
wrap!(jsapi: pub fn JS_ReadTypedArray(r: *mut JSStructuredCloneReader, vp: MutableHandleValue) -> bool);
336-
wrap!(jsapi: pub fn JS_WriteString(w: *mut JSStructuredCloneWriter, str: HandleString) -> bool);
336+
wrap!(jsapi: pub fn JS_WriteString(w: *mut JSStructuredCloneWriter, str_: HandleString) -> bool);
337337
wrap!(jsapi: pub fn JS_WriteTypedArray(w: *mut JSStructuredCloneWriter, v: HandleValue) -> bool);
338338
wrap!(jsapi: pub fn JS_ObjectNotWritten(w: *mut JSStructuredCloneWriter, obj: HandleObject) -> bool);
339339
wrap!(jsapi: pub fn JS_NewInt8ArrayFromArray(cx: *mut JSContext, array: HandleObject) -> *mut JSObject);

src/jsglue.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class RustJobQueue : public JS::JobQueue{
5959
return mTraps.getIncumbentGlobal(mQueue, cx);
6060
}
6161

62-
bool enqueuePromiseJob(JSContext* cx, JS::HandleObject promise,
62+
virtual bool enqueuePromiseJob(JSContext* cx, JS::HandleObject promise,
6363
JS::HandleObject job, JS::HandleObject allocationSite,
6464
JS::HandleObject incumbentGlobal)
6565
{
@@ -114,12 +114,10 @@ class RustReadableStreamUnderlyingSource : public JS::ReadableStreamUnderlyingSo
114114
return resolve_to;
115115
}
116116

117-
118117
virtual void onClosed(JSContext* cx, JS::HandleObject stream) {
119118
return mTraps.onClosed(mSource, cx, stream);
120119
}
121120

122-
123121
virtual void onErrored(JSContext* cx, JS::HandleObject stream, JS::HandleValue reason) {
124122
return mTraps.onErrored(mSource, cx, stream, reason);
125123
}
@@ -129,6 +127,30 @@ class RustReadableStreamUnderlyingSource : public JS::ReadableStreamUnderlyingSo
129127
}
130128
};
131129

130+
struct JSExternalStringCallbacksTraps {
131+
void (*finalize)(void* privateData, char16_t* chars);
132+
size_t (*sizeOfBuffer)(void* privateData, const char16_t* chars, mozilla::MallocSizeOf mallocSizeOf);
133+
};
134+
135+
class RustJSExternalStringCallbacks final : public JSExternalStringCallbacks {
136+
JSExternalStringCallbacksTraps mTraps;
137+
void *privateData;
138+
public:
139+
RustJSExternalStringCallbacks(const JSExternalStringCallbacksTraps& aTraps, void* privateData)
140+
: mTraps(aTraps)
141+
, privateData(privateData)
142+
{
143+
}
144+
145+
void finalize(char16_t* chars) const override {
146+
return mTraps.finalize(privateData, chars);
147+
}
148+
149+
size_t sizeOfBuffer(const char16_t* chars, mozilla::MallocSizeOf mallocSizeOf) const override {
150+
return mTraps.sizeOfBuffer(privateData, chars, mallocSizeOf);
151+
}
152+
};
153+
132154
struct ProxyTraps {
133155
bool (*enter)(JSContext *cx, JS::HandleObject proxy, JS::HandleId id,
134156
js::BaseProxyHandler::Action action, bool *bp);
@@ -556,7 +578,6 @@ class RustJSPrincipals final : public JSPrincipals
556578
: false;
557579
}
558580

559-
560581
bool isSystemOrAddonPrincipal() override {
561582
return this->callbacks.isSystemOrAddonPrincipal(this);
562583
}
@@ -1222,6 +1243,17 @@ DeleteReadableStreamUnderlyingSource(JS::ReadableStreamUnderlyingSource* source)
12221243
delete source;
12231244
}
12241245

1246+
JSExternalStringCallbacks*
1247+
CreateRustJSExternalStringCallbacks(const JSExternalStringCallbacksTraps* aTraps, void* aCallbacks)
1248+
{
1249+
return new RustJSExternalStringCallbacks(*aTraps, aCallbacks);
1250+
}
1251+
1252+
void DeleteRustJSExternalStringCallbacks(JSExternalStringCallbacks* callbacks)
1253+
{
1254+
delete static_cast<RustJSExternalStringCallbacks*>(callbacks);
1255+
}
1256+
12251257
void
12261258
DispatchableRun(JSContext* cx, JS::Dispatchable* ptr, JS::Dispatchable::MaybeShuttingDown mb) {
12271259
ptr->run(cx, mb);

0 commit comments

Comments
 (0)