diff options
author | 2022-12-16 00:40:08 -0800 | |
---|---|---|
committer | 2022-12-16 00:40:08 -0800 | |
commit | 01ed3045cb436085d7815bc089d4db21b4095b33 (patch) | |
tree | 3824d3fb7872abca6afd205746f7433b7def06e3 /src/bun.js | |
parent | 6b3be4d821f89a5edef3308dad4d944f1a5e42d0 (diff) | |
download | bun-01ed3045cb436085d7815bc089d4db21b4095b33.tar.gz bun-01ed3045cb436085d7815bc089d4db21b4095b33.tar.zst bun-01ed3045cb436085d7815bc089d4db21b4095b33.zip |
[napi] Implement `napi_create_bigint_words`
warning: not tested
Diffstat (limited to 'src/bun.js')
-rw-r--r-- | src/bun.js/bindings/napi.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/bun.js/bindings/napi.cpp b/src/bun.js/bindings/napi.cpp index 943f37688..96489f081 100644 --- a/src/bun.js/bindings/napi.cpp +++ b/src/bun.js/bindings/napi.cpp @@ -46,6 +46,7 @@ #include "JavaScriptCore/GetterSetter.h" #include "JavaScriptCore/JSSourceCode.h" #include "JavaScriptCore/JSNativeStdFunction.h" +#include "JavaScriptCore/BigIntObject.h" #include "../modules/ObjectModule.h" @@ -1507,4 +1508,33 @@ extern "C" napi_status napi_set_instance_data(napi_env env, globalObject->napiInstanceDataFinalizerHint = finalize_hint; return napi_ok; +} + +extern "C" napi_status napi_create_bigint_words(napi_env env, + int sign_bit, + size_t word_count, + const uint64_t* words, + napi_value* result) +{ + Zig::GlobalObject* globalObject = toJS(env); + JSC::VM& vm = globalObject->vm(); + auto* bigint = JSC::JSBigInt::tryCreateWithLength(vm, word_count); + if (UNLIKELY(!bigint)) { + return napi_generic_failure; + } + + // TODO: verify sign bit is consistent + bigint->setSign(sign_bit); + + if (words != nullptr) { + const uint64_t* word = words; + // TODO: add fast path that uses memcpy here instead of setDigit + // we need to add this to JSC. V8 has this optimization + for (size_t i = 0; i < word_count; i++) { + bigint->setDigit(i, *word++); + } + } + + *result = toNapi(bigint); + return napi_ok; }
\ No newline at end of file |