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/bindings/ZigGlobalObject.cpp | |
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/bindings/ZigGlobalObject.cpp')
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.cpp | 22 |
1 files changed, 21 insertions, 1 deletions
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(), |