allow zig js host functions to return JSError (#15120)

This commit is contained in:
Meghan Denny
2024-11-13 21:11:56 -08:00
committed by GitHub
parent 32ddf343ee
commit fdd8d35845
70 changed files with 827 additions and 876 deletions

View File

@@ -12,7 +12,7 @@ const EVP = Crypto.EVP;
const PBKDF2 = EVP.PBKDF2;
const JSValue = JSC.JSValue;
const validators = @import("./util/validators.zig");
fn randomInt(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSValue {
fn randomInt(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSC.JSValue {
const arguments = callframe.arguments(2).slice();
//min, max
@@ -41,7 +41,7 @@ fn randomInt(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSC.JSV
fn pbkdf2(
globalThis: *JSC.JSGlobalObject,
callframe: *JSC.CallFrame,
) JSC.JSValue {
) bun.JSError!JSC.JSValue {
const arguments = callframe.arguments(5);
const data = PBKDF2.fromJS(globalThis, arguments.slice(), true) orelse {
@@ -56,7 +56,7 @@ fn pbkdf2(
fn pbkdf2Sync(
globalThis: *JSC.JSGlobalObject,
callframe: *JSC.CallFrame,
) JSC.JSValue {
) bun.JSError!JSC.JSValue {
const arguments = callframe.arguments(5);
var data = PBKDF2.fromJS(globalThis, arguments.slice(), false) orelse {
@@ -86,16 +86,12 @@ fn pbkdf2Sync(
return out_arraybuffer;
}
const jsPbkdf2 = JSC.toJSHostFunction(pbkdf2);
const jsPbkdf2Sync = JSC.toJSHostFunction(pbkdf2Sync);
const jsRandomInt = JSC.toJSHostFunction(randomInt);
pub fn createNodeCryptoBindingZig(global: *JSC.JSGlobalObject) JSC.JSValue {
const crypto = JSC.JSValue.createEmptyObject(global, 3);
crypto.put(global, bun.String.init("pbkdf2"), JSC.JSFunction.create(global, "pbkdf2", jsPbkdf2, 5, .{}));
crypto.put(global, bun.String.init("pbkdf2Sync"), JSC.JSFunction.create(global, "pbkdf2Sync", jsPbkdf2Sync, 5, .{}));
crypto.put(global, bun.String.init("randomInt"), JSC.JSFunction.create(global, "randomInt", jsRandomInt, 2, .{}));
crypto.put(global, bun.String.init("pbkdf2"), JSC.JSFunction.create(global, "pbkdf2", pbkdf2, 5, .{}));
crypto.put(global, bun.String.init("pbkdf2Sync"), JSC.JSFunction.create(global, "pbkdf2Sync", pbkdf2Sync, 5, .{}));
crypto.put(global, bun.String.init("randomInt"), JSC.JSFunction.create(global, "randomInt", randomInt, 2, .{}));
return crypto;
}