MCPcopy Create free account
hub / github.com/Snapchat/Valdi / valueToTypedArray

Method valueToTypedArray

valdi/src/valdi/quickjs/QuickJSJavaScriptContext.cpp:881–919  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

879}
880
881Valdi::JSTypedArray QuickJSJavaScriptContext::valueToTypedArray(const Valdi::JSValue& value,
882 Valdi::JSExceptionTracker& exceptionTracker) {
883 auto guard = _threadAccessChecker.guard();
884 auto jsValue = fromValdiJSValue(value);
885
886 if (JS_IsArrayBuffer(_context, jsValue) != 0) {
887 size_t size = 0;
888 auto* buffer = JS_GetArrayBuffer(_context, &size, jsValue);
889
890 if (!checkCall(exceptionTracker, buffer == nullptr ? -1 : 0)) {
891 return Valdi::JSTypedArray();
892 }
893
894 return Valdi::JSTypedArray(Valdi::ArrayBuffer, buffer, size, toUnretainedJSValueRef(jsValue));
895 } else {
896 auto arrayType = JS_GetTypedArrayType(_context, jsValue);
897 if (arrayType == JS_TYPED_ARRAY_NONE) {
898 checkCallAndGetValue(exceptionTracker, JS_ThrowTypeError(_context, "value is not a typed array"));
899 return Valdi::JSTypedArray();
900 }
901
902 auto arrayBuffer = checkCallAndGetValue(exceptionTracker,
903 JS_GetTypedArrayBuffer(_context, jsValue, nullptr, nullptr, nullptr));
904 if (!exceptionTracker) {
905 return Valdi::JSTypedArray();
906 }
907
908 size_t length = 0;
909 size_t bytesLength = 0;
910 auto* ptr = JS_GetTypedArrayData(_context, jsValue, &length, &bytesLength);
911 if (!checkCall(exceptionTracker, ptr == nullptr ? -1 : 0)) {
912 return Valdi::JSTypedArray();
913 }
914
915 auto type = toValdiTypedArrayType(arrayType);
916
917 return Valdi::JSTypedArray(type, ptr, static_cast<size_t>(bytesLength), std::move(arrayBuffer));
918 }
919}
920
921Valdi::ValueType QuickJSJavaScriptContext::getValueType(const Valdi::JSValue& value) {
922 auto guard = _threadAccessChecker.guard();

Callers

nothing calls this directly

Calls 4

JSTypedArrayClass · 0.85
toValdiTypedArrayTypeFunction · 0.85
guardMethod · 0.80
fromValdiJSValueFunction · 0.70

Tested by

no test coverage detected