diff options
author | 2022-09-14 04:12:32 -0700 | |
---|---|---|
committer | 2022-09-14 04:12:32 -0700 | |
commit | 7bfa302b75c2450a872dc6b5de0002a9c7959ea9 (patch) | |
tree | c01a806611a0cbff3c47cfb9bc098d14e7d1667c /src/bun.js | |
parent | 0935ab14d6cad3a3c80bee5b585381cfbbe74699 (diff) | |
download | bun-7bfa302b75c2450a872dc6b5de0002a9c7959ea9.tar.gz bun-7bfa302b75c2450a872dc6b5de0002a9c7959ea9.tar.zst bun-7bfa302b75c2450a872dc6b5de0002a9c7959ea9.zip |
Make `crypto.getRandomValues()` faster + fix > 1 byte/element typed arrays
Fix crypto.getRandomValues() with > 1 byte element typed arrays
Fixes https://github.com/oven-sh/bun/issues/1237
Diffstat (limited to 'src/bun.js')
-rw-r--r-- | src/bun.js/base.zig | 17 | ||||
-rw-r--r-- | src/bun.js/bindings/ZigGeneratedCode.cpp | 82 | ||||
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.cpp | 22 | ||||
-rw-r--r-- | src/bun.js/bindings/bindings.zig | 3 | ||||
-rw-r--r-- | src/bun.js/bindings/headers-cpp.h | 8 | ||||
-rw-r--r-- | src/bun.js/bindings/headers.h | 18 | ||||
-rw-r--r-- | src/bun.js/bindings/headers.zig | 2 | ||||
-rw-r--r-- | src/bun.js/javascript.zig | 2 | ||||
-rw-r--r-- | src/bun.js/webcore.zig | 85 |
9 files changed, 188 insertions, 51 deletions
diff --git a/src/bun.js/base.zig b/src/bun.js/base.zig index eb3b84118..2ded4e5cb 100644 --- a/src/bun.js/base.zig +++ b/src/bun.js/base.zig @@ -3152,7 +3152,9 @@ pub fn DOMCall( } { switch (Fields.len - 2) { - 0 => @compileError("Must be > 0 arguments"), + 0 => { + try writer.writeAll("));\n"); + }, 1 => { try writer.writeAll(", "); try writer.writeAll(DOMCallArgumentTypeWrapper(Fields[2].field_type)); @@ -3178,7 +3180,9 @@ pub fn DOMCall( } { switch (Fields.len - 2) { - 0 => @compileError("Must be > 0 arguments"), + 0 => { + try writer.writeAll(")) {\n"); + }, 1 => { try writer.writeAll(", "); try writer.writeAll(DOMCallArgumentTypeWrapper(Fields[2].field_type)); @@ -3206,7 +3210,9 @@ pub fn DOMCall( } { switch (Fields.len - 2) { - 0 => @compileError("Must be > 0 arguments"), + 0 => { + try writer.writeAll(");\n}\n"); + }, 1 => { try writer.writeAll(", arg1);\n}\n"); }, @@ -3267,16 +3273,17 @@ pub fn DOMCall( { try writer.writeAll(DOMCallResultType(ResultType)); - try writer.writeAll(",\n "); } switch (Fields.len - 2) { - 0 => @compileError("Must be > 0 arguments"), + 0 => {}, 1 => { + try writer.writeAll(",\n "); try writer.writeAll(DOMCallArgumentType(Fields[2].field_type)); try writer.writeAll("\n "); }, 2 => { + try writer.writeAll(",\n "); try writer.writeAll(DOMCallArgumentType(Fields[2].field_type)); try writer.writeAll(",\n "); try writer.writeAll(DOMCallArgumentType(Fields[3].field_type)); diff --git a/src/bun.js/bindings/ZigGeneratedCode.cpp b/src/bun.js/bindings/ZigGeneratedCode.cpp index 25ed067a2..dcd986780 100644 --- a/src/bun.js/bindings/ZigGeneratedCode.cpp +++ b/src/bun.js/bindings/ZigGeneratedCode.cpp @@ -573,5 +573,87 @@ extern "C" void Reader__f64__put(JSC::JSGlobalObject *globalObject, JSC::Encoded } + +extern "C" JSC_DECLARE_HOST_FUNCTION(Crypto__getRandomValues__slowpathWrapper); +extern "C" JSC_DECLARE_JIT_OPERATION_WITHOUT_WTF_INTERNAL(Crypto__getRandomValues__fastpathWrapper, EncodedJSValue, (JSC::JSGlobalObject* lexicalGlobalObject, void* thisValue, JSC::JSUint8Array*)); + +JSC_DEFINE_JIT_OPERATION(Crypto__getRandomValues__fastpathWrapper, EncodedJSValue, (JSC::JSGlobalObject* lexicalGlobalObject, void* thisValue, JSC::JSUint8Array* arg1)) { +VM& vm = JSC::getVM(lexicalGlobalObject); +IGNORE_WARNINGS_BEGIN("frame-address") +CallFrame* callFrame = DECLARE_CALL_FRAME(vm); +IGNORE_WARNINGS_END +JSC::JITOperationPrologueCallFrameTracer tracer(vm, callFrame); +return Crypto__getRandomValues__fastpath(lexicalGlobalObject, thisValue, arg1); +} +JSC_DEFINE_HOST_FUNCTION(Crypto__getRandomValues__slowpathWrapper, (JSC::JSGlobalObject *globalObject, JSC::CallFrame* frame)) { + return Crypto__getRandomValues__slowpath(globalObject, JSValue::encode(frame->thisValue()), reinterpret_cast<JSC::EncodedJSValue*>(frame->addressOfArgumentsStart()), frame->argumentCount()); +} + +extern "C" void Crypto__getRandomValues__put(JSC::JSGlobalObject *globalObject, JSC::EncodedJSValue value) { + JSC::JSObject *thisObject = JSC::jsCast<JSC::JSObject *>(JSC::JSValue::decode(value)); + static const JSC::DOMJIT::Signature DOMJIT_getRandomValues_signature( + Crypto__getRandomValues__fastpathWrapper, + thisObject->classInfo(), + JSC::DOMJIT::Effect::forReadWrite(JSC::DOMJIT::HeapRange::top(), JSC::DOMJIT::HeapRange::top()), + JSC::SpecHeapTop, + JSC::SpecUint8Array + ); + JSFunction* function = JSFunction::create( + globalObject->vm(), + globalObject, + 1, + String("getRandomValues"_s), + Crypto__getRandomValues__slowpathWrapper, ImplementationVisibility::Public, NoIntrinsic, Crypto__getRandomValues__slowpathWrapper, + &DOMJIT_getRandomValues_signature + ); + thisObject->putDirect( + globalObject->vm(), + Identifier::fromString(globalObject->vm(), "getRandomValues"_s), + function, + JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DOMJITFunction | 0 + ); +} + + + +extern "C" JSC_DECLARE_HOST_FUNCTION(Crypto__randomUUID__slowpathWrapper); +extern "C" JSC_DECLARE_JIT_OPERATION_WITHOUT_WTF_INTERNAL(Crypto__randomUUID__fastpathWrapper, EncodedJSValue, (JSC::JSGlobalObject* lexicalGlobalObject, void* thisValue)); + +JSC_DEFINE_JIT_OPERATION(Crypto__randomUUID__fastpathWrapper, EncodedJSValue, (JSC::JSGlobalObject* lexicalGlobalObject, void* thisValue)) { +VM& vm = JSC::getVM(lexicalGlobalObject); +IGNORE_WARNINGS_BEGIN("frame-address") +CallFrame* callFrame = DECLARE_CALL_FRAME(vm); +IGNORE_WARNINGS_END +JSC::JITOperationPrologueCallFrameTracer tracer(vm, callFrame); +return Crypto__randomUUID__fastpath(lexicalGlobalObject, thisValue); +} +JSC_DEFINE_HOST_FUNCTION(Crypto__randomUUID__slowpathWrapper, (JSC::JSGlobalObject *globalObject, JSC::CallFrame* frame)) { + return Crypto__randomUUID__slowpath(globalObject, JSValue::encode(frame->thisValue()), reinterpret_cast<JSC::EncodedJSValue*>(frame->addressOfArgumentsStart()), frame->argumentCount()); +} + +extern "C" void Crypto__randomUUID__put(JSC::JSGlobalObject *globalObject, JSC::EncodedJSValue value) { + JSC::JSObject *thisObject = JSC::jsCast<JSC::JSObject *>(JSC::JSValue::decode(value)); + static const JSC::DOMJIT::Signature DOMJIT_randomUUID_signature( + Crypto__randomUUID__fastpathWrapper, + thisObject->classInfo(), + JSC::DOMJIT::Effect::forReadWrite(JSC::DOMJIT::HeapRange::top(), JSC::DOMJIT::HeapRange::top()), + JSC::SpecString); + JSFunction* function = JSFunction::create( + globalObject->vm(), + globalObject, + 0, + String("randomUUID"_s), + Crypto__randomUUID__slowpathWrapper, ImplementationVisibility::Public, NoIntrinsic, Crypto__randomUUID__slowpathWrapper, + &DOMJIT_randomUUID_signature + ); + thisObject->putDirect( + globalObject->vm(), + Identifier::fromString(globalObject->vm(), "randomUUID"_s), + function, + JSC::PropertyAttribute::Function | JSC::PropertyAttribute::DOMJITFunction | 0 + ); +} + + /* -- END DOMCall DEFINITIONS-- */ diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index 052fc3e7b..b7fc91c51 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -2303,6 +2303,10 @@ void GlobalObject::addBuiltinGlobals(JSC::VM& vm) // putDirect(vm, static_cast<JSVMClientData*>(vm.clientData)->builtinNames().nativeReadableStreamPrototypePrivateName(), jsUndefined(), JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::DontEnum | 0); } +// We set it in here since it's a global +extern "C" void Crypto__randomUUID__put(JSC::JSGlobalObject* globalObject, JSC::EncodedJSValue value); +extern "C" void Crypto__getRandomValues__put(JSC::JSGlobalObject* globalObject, JSC::EncodedJSValue value); + // This is not a publicly exposed API currently. // This is used by the bundler to make Response, Request, FetchEvent, // and any other objects available globally. @@ -2428,7 +2432,23 @@ void GlobalObject::installAPIGlobals(JSClassRef* globals, int count, JSC::VM& vm JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0 }); } - for (j = 1; j < count - 1; j++) { + { + j = 1; + auto jsClass = globals[j]; + + JSC::JSCallbackObject<JSNonFinalObject>* object = JSC::JSCallbackObject<JSNonFinalObject>::create(this, this->callbackObjectStructure(), + jsClass, nullptr); + if (JSObject* prototype = object->classRef()->prototype(this)) + object->setPrototypeDirect(vm, prototype); + + Crypto__getRandomValues__put(this, JSValue::encode(object)); + Crypto__randomUUID__put(this, JSValue::encode(object)); + extraStaticGlobals.uncheckedAppend( + GlobalPropertyInfo { JSC::Identifier::fromString(vm, jsClass->className()), + JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0 }); + } + + for (j = 2; j < count - 1; j++) { auto jsClass = globals[j]; JSC::JSCallbackObject<JSNonFinalObject>* object = JSC::JSCallbackObject<JSNonFinalObject>::create(this, this->callbackObjectStructure(), diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 00eb59cea..cf16a0934 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -4225,7 +4225,10 @@ pub const __DOMCall__reader_f64 = @import("../api/bun.zig").FFI.Reader.Class.fun pub const __DOMCall__reader_i64 = @import("../api/bun.zig").FFI.Reader.Class.functionDefinitions.@"i64"; pub const __DOMCall__reader_u64 = @import("../api/bun.zig").FFI.Reader.Class.functionDefinitions.@"u64"; pub const __DOMCall__reader_intptr = @import("../api/bun.zig").FFI.Reader.Class.functionDefinitions.@"intptr"; +pub const __Crypto_getRandomValues = @import("../webcore.zig").Crypto.Class.functionDefinitions.@"getRandomValues"; +pub const __Crypto_randomUUID = @import("../webcore.zig").Crypto.Class.functionDefinitions.@"randomUUID"; pub const DOMCalls = .{ @import("../api/bun.zig").FFI, @import("../api/bun.zig").FFI.Reader, + @import("../webcore.zig").Crypto, }; diff --git a/src/bun.js/bindings/headers-cpp.h b/src/bun.js/bindings/headers-cpp.h index ac32a6db9..550379eef 100644 --- a/src/bun.js/bindings/headers-cpp.h +++ b/src/bun.js/bindings/headers-cpp.h @@ -1,4 +1,4 @@ -//-- AUTOGENERATED FILE -- 1662772023 +//-- AUTOGENERATED FILE -- 1663152297 // clang-format off #pragma once @@ -240,8 +240,8 @@ extern "C" const size_t Zig__ConsoleClient_object_align_ = alignof(Zig::ConsoleC extern "C" const size_t Bun__Timer_object_size_ = sizeof(Bun__Timer); extern "C" const size_t Bun__Timer_object_align_ = alignof(Bun__Timer); -const size_t sizes[45] = {sizeof(JSC::JSObject), sizeof(WebCore::DOMURL), sizeof(WebCore::FetchHeaders), sizeof(SystemError), sizeof(JSC::JSCell), sizeof(JSC::JSString), sizeof(Inspector::ScriptArguments), sizeof(JSC::JSModuleLoader), sizeof(JSC::JSModuleRecord), sizeof(JSC::JSPromise), sizeof(JSC::JSInternalPromise), sizeof(JSC::SourceOrigin), sizeof(JSC::SourceCode), sizeof(JSC::JSFunction), sizeof(JSC::JSGlobalObject), sizeof(WTF::URL), sizeof(WTF::String), sizeof(JSC::JSValue), sizeof(JSC::PropertyName), sizeof(JSC::Exception), sizeof(JSC::VM), sizeof(JSC::ThrowScope), sizeof(JSC::CatchScope), sizeof(JSC::Identifier), sizeof(WTF::StringImpl), sizeof(WTF::ExternalStringImpl), sizeof(WTF::StringView), sizeof(FFI__ptr), sizeof(Reader__u8), sizeof(Reader__u16), sizeof(Reader__u32), sizeof(Reader__ptr), sizeof(Reader__i8), sizeof(Reader__i16), sizeof(Reader__i32), sizeof(Reader__f32), sizeof(Reader__f64), sizeof(Reader__i64), sizeof(Reader__u64), sizeof(Reader__intptr), sizeof(Zig::GlobalObject), sizeof(Bun__Path), sizeof(ArrayBufferSink), sizeof(HTTPSResponseSink), sizeof(HTTPResponseSink)}; +const size_t sizes[47] = {sizeof(JSC::JSObject), sizeof(WebCore::DOMURL), sizeof(WebCore::FetchHeaders), sizeof(SystemError), sizeof(JSC::JSCell), sizeof(JSC::JSString), sizeof(Inspector::ScriptArguments), sizeof(JSC::JSModuleLoader), sizeof(JSC::JSModuleRecord), sizeof(JSC::JSPromise), sizeof(JSC::JSInternalPromise), sizeof(JSC::SourceOrigin), sizeof(JSC::SourceCode), sizeof(JSC::JSFunction), sizeof(JSC::JSGlobalObject), sizeof(WTF::URL), sizeof(WTF::String), sizeof(JSC::JSValue), sizeof(JSC::PropertyName), sizeof(JSC::Exception), sizeof(JSC::VM), sizeof(JSC::ThrowScope), sizeof(JSC::CatchScope), sizeof(JSC::Identifier), sizeof(WTF::StringImpl), sizeof(WTF::ExternalStringImpl), sizeof(WTF::StringView), sizeof(FFI__ptr), sizeof(Reader__u8), sizeof(Reader__u16), sizeof(Reader__u32), sizeof(Reader__ptr), sizeof(Reader__i8), sizeof(Reader__i16), sizeof(Reader__i32), sizeof(Reader__f32), sizeof(Reader__f64), sizeof(Reader__i64), sizeof(Reader__u64), sizeof(Reader__intptr), sizeof(Crypto__getRandomValues), sizeof(Crypto__randomUUID), sizeof(Zig::GlobalObject), sizeof(Bun__Path), sizeof(ArrayBufferSink), sizeof(HTTPSResponseSink), sizeof(HTTPResponseSink)}; -const char* names[45] = {"JSC__JSObject", "WebCore__DOMURL", "WebCore__FetchHeaders", "SystemError", "JSC__JSCell", "JSC__JSString", "Inspector__ScriptArguments", "JSC__JSModuleLoader", "JSC__JSModuleRecord", "JSC__JSPromise", "JSC__JSInternalPromise", "JSC__SourceOrigin", "JSC__SourceCode", "JSC__JSFunction", "JSC__JSGlobalObject", "WTF__URL", "WTF__String", "JSC__JSValue", "JSC__PropertyName", "JSC__Exception", "JSC__VM", "JSC__ThrowScope", "JSC__CatchScope", "JSC__Identifier", "WTF__StringImpl", "WTF__ExternalStringImpl", "WTF__StringView", "FFI__ptr", "Reader__u8", "Reader__u16", "Reader__u32", "Reader__ptr", "Reader__i8", "Reader__i16", "Reader__i32", "Reader__f32", "Reader__f64", "Reader__i64", "Reader__u64", "Reader__intptr", "Zig__GlobalObject", "Bun__Path", "ArrayBufferSink", "HTTPSResponseSink", "HTTPResponseSink"}; +const char* names[47] = {"JSC__JSObject", "WebCore__DOMURL", "WebCore__FetchHeaders", "SystemError", "JSC__JSCell", "JSC__JSString", "Inspector__ScriptArguments", "JSC__JSModuleLoader", "JSC__JSModuleRecord", "JSC__JSPromise", "JSC__JSInternalPromise", "JSC__SourceOrigin", "JSC__SourceCode", "JSC__JSFunction", "JSC__JSGlobalObject", "WTF__URL", "WTF__String", "JSC__JSValue", "JSC__PropertyName", "JSC__Exception", "JSC__VM", "JSC__ThrowScope", "JSC__CatchScope", "JSC__Identifier", "WTF__StringImpl", "WTF__ExternalStringImpl", "WTF__StringView", "FFI__ptr", "Reader__u8", "Reader__u16", "Reader__u32", "Reader__ptr", "Reader__i8", "Reader__i16", "Reader__i32", "Reader__f32", "Reader__f64", "Reader__i64", "Reader__u64", "Reader__intptr", "Crypto__getRandomValues", "Crypto__randomUUID", "Zig__GlobalObject", "Bun__Path", "ArrayBufferSink", "HTTPSResponseSink", "HTTPResponseSink"}; -const size_t aligns[45] = {alignof(JSC::JSObject), alignof(WebCore::DOMURL), alignof(WebCore::FetchHeaders), alignof(SystemError), alignof(JSC::JSCell), alignof(JSC::JSString), alignof(Inspector::ScriptArguments), alignof(JSC::JSModuleLoader), alignof(JSC::JSModuleRecord), alignof(JSC::JSPromise), alignof(JSC::JSInternalPromise), alignof(JSC::SourceOrigin), alignof(JSC::SourceCode), alignof(JSC::JSFunction), alignof(JSC::JSGlobalObject), alignof(WTF::URL), alignof(WTF::String), alignof(JSC::JSValue), alignof(JSC::PropertyName), alignof(JSC::Exception), alignof(JSC::VM), alignof(JSC::ThrowScope), alignof(JSC::CatchScope), alignof(JSC::Identifier), alignof(WTF::StringImpl), alignof(WTF::ExternalStringImpl), alignof(WTF::StringView), alignof(FFI__ptr), alignof(Reader__u8), alignof(Reader__u16), alignof(Reader__u32), alignof(Reader__ptr), alignof(Reader__i8), alignof(Reader__i16), alignof(Reader__i32), alignof(Reader__f32), alignof(Reader__f64), alignof(Reader__i64), alignof(Reader__u64), alignof(Reader__intptr), alignof(Zig::GlobalObject), alignof(Bun__Path), alignof(ArrayBufferSink), alignof(HTTPSResponseSink), alignof(HTTPResponseSink)}; +const size_t aligns[47] = {alignof(JSC::JSObject), alignof(WebCore::DOMURL), alignof(WebCore::FetchHeaders), alignof(SystemError), alignof(JSC::JSCell), alignof(JSC::JSString), alignof(Inspector::ScriptArguments), alignof(JSC::JSModuleLoader), alignof(JSC::JSModuleRecord), alignof(JSC::JSPromise), alignof(JSC::JSInternalPromise), alignof(JSC::SourceOrigin), alignof(JSC::SourceCode), alignof(JSC::JSFunction), alignof(JSC::JSGlobalObject), alignof(WTF::URL), alignof(WTF::String), alignof(JSC::JSValue), alignof(JSC::PropertyName), alignof(JSC::Exception), alignof(JSC::VM), alignof(JSC::ThrowScope), alignof(JSC::CatchScope), alignof(JSC::Identifier), alignof(WTF::StringImpl), alignof(WTF::ExternalStringImpl), alignof(WTF::StringView), alignof(FFI__ptr), alignof(Reader__u8), alignof(Reader__u16), alignof(Reader__u32), alignof(Reader__ptr), alignof(Reader__i8), alignof(Reader__i16), alignof(Reader__i32), alignof(Reader__f32), alignof(Reader__f64), alignof(Reader__i64), alignof(Reader__u64), alignof(Reader__intptr), alignof(Crypto__getRandomValues), alignof(Crypto__randomUUID), alignof(Zig::GlobalObject), alignof(Bun__Path), alignof(ArrayBufferSink), alignof(HTTPSResponseSink), alignof(HTTPResponseSink)}; diff --git a/src/bun.js/bindings/headers.h b/src/bun.js/bindings/headers.h index 9f1b43c8c..293abf53f 100644 --- a/src/bun.js/bindings/headers.h +++ b/src/bun.js/bindings/headers.h @@ -1,5 +1,5 @@ // clang-format off -//-- AUTOGENERATED FILE -- 1662772023 +//-- AUTOGENERATED FILE -- 1663152297 #pragma once #include <stddef.h> @@ -750,6 +750,22 @@ ZIG_DECL JSC__JSValue Reader__intptr__fastpath(JSC__JSGlobalObject* arg0, void* ZIG_DECL JSC__JSValue Reader__intptr__slowpath(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1, JSC__JSValue* arg2, size_t arg3); #endif +CPP_DECL void Crypto__getRandomValues__put(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1); + +#ifdef __cplusplus + +ZIG_DECL JSC__JSValue Crypto__getRandomValues__fastpath(JSC__JSGlobalObject* arg0, void* arg1, Uint8Array_alias* arg2); +ZIG_DECL JSC__JSValue Crypto__getRandomValues__slowpath(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1, JSC__JSValue* arg2, size_t arg3); + +#endif +CPP_DECL void Crypto__randomUUID__put(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1); + +#ifdef __cplusplus + +ZIG_DECL JSC__JSValue Crypto__randomUUID__fastpath(JSC__JSGlobalObject* arg0, void* arg1); +ZIG_DECL JSC__JSValue Crypto__randomUUID__slowpath(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1, JSC__JSValue* arg2, size_t arg3); + +#endif #pragma mark - Zig::GlobalObject diff --git a/src/bun.js/bindings/headers.zig b/src/bun.js/bindings/headers.zig index 884dcbb40..819b39ad9 100644 --- a/src/bun.js/bindings/headers.zig +++ b/src/bun.js/bindings/headers.zig @@ -423,6 +423,8 @@ pub extern fn Reader__f64__put(arg0: ?*JSC__JSGlobalObject, JSValue1: JSC__JSVal pub extern fn Reader__i64__put(arg0: ?*JSC__JSGlobalObject, JSValue1: JSC__JSValue) void; pub extern fn Reader__u64__put(arg0: ?*JSC__JSGlobalObject, JSValue1: JSC__JSValue) void; pub extern fn Reader__intptr__put(arg0: ?*JSC__JSGlobalObject, JSValue1: JSC__JSValue) void; +pub extern fn Crypto__getRandomValues__put(arg0: ?*JSC__JSGlobalObject, JSValue1: JSC__JSValue) void; +pub extern fn Crypto__randomUUID__put(arg0: ?*JSC__JSGlobalObject, JSValue1: JSC__JSValue) void; pub extern fn Zig__GlobalObject__create(arg0: [*c]JSClassRef, arg1: i32, arg2: ?*anyopaque) ?*JSC__JSGlobalObject; pub extern fn Zig__GlobalObject__getModuleRegistryMap(arg0: ?*JSC__JSGlobalObject) ?*anyopaque; pub extern fn Zig__GlobalObject__resetModuleRegistryMap(arg0: ?*JSC__JSGlobalObject, arg1: ?*anyopaque) bool; diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index 6e3e80570..5eeff1ba8 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -94,6 +94,7 @@ pub const GlobalConstructors = [_]type{ pub const GlobalClasses = [_]type{ Bun.Class, + WebCore.Crypto.Class, EventListenerMixin.addEventListener(VirtualMachine), BuildError.Class, ResolveError.Class, @@ -101,7 +102,6 @@ pub const GlobalClasses = [_]type{ Fetch.Class, js_ast.Macro.JSNode.BunJSXCallbackFunction, - WebCore.Crypto.Class, WebCore.Crypto.Prototype, WebCore.Alert.Class, diff --git a/src/bun.js/webcore.zig b/src/bun.js/webcore.zig index b134fb1c9..ac5238dac 100644 --- a/src/bun.js/webcore.zig +++ b/src/bun.js/webcore.zig @@ -360,12 +360,8 @@ pub const Crypto = struct { const UUID = @import("./uuid.zig"); pub const Class = JSC.NewClass(void, .{ .name = "crypto" }, .{ - .getRandomValues = .{ - .rfn = getRandomValues, - }, - .randomUUID = .{ - .rfn = randomUUID, - }, + .getRandomValues = JSC.DOMCall("Crypto", @This(), "getRandomValues", JSC.JSValue, JSC.DOMEffect.top), + .randomUUID = JSC.DOMCall("Crypto", @This(), "randomUUID", *JSC.JSString, JSC.DOMEffect.top), }, .{}); pub const Prototype = JSC.NewClass( void, @@ -379,49 +375,63 @@ pub const Crypto = struct { ); pub fn getRandomValues( - // this - _: void, - ctx: JSC.C.JSContextRef, - // function - _: JSC.C.JSObjectRef, - // thisObject - _: JSC.C.JSObjectRef, - arguments: []const JSC.C.JSValueRef, - exception: JSC.C.ExceptionRef, - ) JSC.C.JSValueRef { + globalThis: *JSC.JSGlobalObject, + _: JSC.JSValue, + arguments: []const JSC.JSValue, + ) JSC.JSValue { if (arguments.len == 0) { - JSC.JSError(JSC.getAllocator(ctx), "Expected typed array but received nothing", .{}, ctx, exception); - return JSC.JSValue.jsUndefined().asObjectRef(); + globalThis.throwInvalidArguments("Expected typed array but got nothing", .{}); + return JSC.JSValue.jsUndefined(); } - var array_buffer = JSC.MarkedArrayBuffer.fromJS(ctx.ptr(), JSC.JSValue.fromRef(arguments[0]), exception) orelse { - JSC.JSError(JSC.getAllocator(ctx), "Expected typed array", .{}, ctx, exception); - return JSC.JSValue.jsUndefined().asObjectRef(); + + var array_buffer = arguments[0].asArrayBuffer(globalThis) orelse { + globalThis.throwInvalidArguments("Expected typed array but got {s}", .{@tagName(arguments[0].jsType())}); + return JSC.JSValue.jsUndefined(); }; - var slice = array_buffer.slice(); + var slice = array_buffer.byteSlice(); if (slice.len > 0) std.crypto.random.bytes(slice); return arguments[0]; } - pub fn call( - // this - _: void, - _: JSC.C.JSContextRef, - // function - _: JSC.C.JSObjectRef, - // thisObject - _: JSC.C.JSObjectRef, - _: []const JSC.C.JSValueRef, - _: JSC.C.ExceptionRef, - ) JSC.C.JSValueRef { - return JSC.JSValue.jsUndefined().asObjectRef(); + pub fn getRandomValuesWithoutTypeChecks( + _: *JSC.JSGlobalObject, + _: *anyopaque, + array: *JSC.JSUint8Array, + ) callconv(.C) JSC.JSValue { + var slice = array.slice(); + if (slice.len > 0) + std.crypto.random.bytes(slice); + + return @intToEnum(JSC.JSValue, @bitCast(i64, @ptrToInt(array))); } pub fn randomUUID( + globalThis: *JSC.JSGlobalObject, + _: JSC.JSValue, + _: []const JSC.JSValue, + ) JSC.JSValue { + var uuid = UUID.init(); + var out: [128]u8 = undefined; + var str = std.fmt.bufPrint(&out, "{s}", .{uuid}) catch unreachable; + return JSC.ZigString.init(str).toValueGC(globalThis); + } + + pub fn randomUUIDWithoutTypeChecks( + globalThis: *JSC.JSGlobalObject, + _: *anyopaque, + ) callconv(.C) JSC.JSValue { + var uuid = UUID.init(); + var out: [128]u8 = undefined; + var str = std.fmt.bufPrint(&out, "{s}", .{uuid}) catch unreachable; + return JSC.ZigString.init(str).toValueGC(globalThis); + } + + pub fn call( // this _: void, - ctx: JSC.C.JSContextRef, + _: JSC.C.JSContextRef, // function _: JSC.C.JSObjectRef, // thisObject @@ -429,9 +439,6 @@ pub const Crypto = struct { _: []const JSC.C.JSValueRef, _: JSC.C.ExceptionRef, ) JSC.C.JSValueRef { - var uuid = UUID.init(); - var out: [128]u8 = undefined; - var str = std.fmt.bufPrint(&out, "{s}", .{uuid}) catch unreachable; - return JSC.ZigString.init(str).toValueGC(ctx.ptr()).asObjectRef(); + return JSC.JSValue.jsUndefined().asObjectRef(); } }; |