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

Method valueToTypedArray

valdi/src/valdi/hermes/HermesJavaScriptContext.cpp:967–998  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

965}
966
967JSTypedArray HermesJavaScriptContext::valueToTypedArray(const JSValue& value, JSExceptionTracker& exceptionTracker) {
968 hermes::vm::GCScope gcScope(*_runtime);
969 auto* hermesValue = toPinnedHermesValue(value);
970
971 if (hermes::vm::vmisa<hermes::vm::JSArrayBuffer>(*hermesValue)) {
972 auto handle = hermes::vm::Handle<hermes::vm::JSArrayBuffer>::vmcast(hermesValue);
973 if (!handle->attached()) {
974 return JSTypedArray();
975 }
976
977 auto* data = handle->getDataBlock(*_runtime);
978 auto size = handle->size();
979
980 return JSTypedArray(TypedArrayType::ArrayBuffer, data, size, JSValueRef::makeUnretained(*this, value));
981 } else if (hermes::vm::vmisa<hermes::vm::JSTypedArrayBase>(*hermesValue)) {
982 auto handle = hermes::vm::Handle<hermes::vm::JSTypedArrayBase>::vmcast(hermesValue);
983 if (!handle->attached(*_runtime)) {
984 return JSTypedArray();
985 }
986
987 auto type = getTypedArrayType(*hermesValue);
988 auto* data = handle->begin(*_runtime);
989 auto size = static_cast<size_t>(handle->getByteLength());
990 auto* arrayBuffer = handle->getBuffer(*_runtime);
991
992 return JSTypedArray(
993 type, data, size, toJSValueRef(hermes::vm::HermesValue::encodeObjectValue(arrayBuffer, *_runtime)));
994 } else {
995 exceptionTracker.onError("Not a TypedArray or ArrayBuffer");
996 return JSTypedArray();
997 }
998}
999
1000static bool hasNativeState(hermes::vm::Runtime& runtime, const hermes::vm::Handle<hermes::vm::JSObject>& jsObject) {
1001 if (!canReceiveNativeState(jsObject.get())) {

Callers

nothing calls this directly

Calls 6

JSTypedArrayClass · 0.85
getTypedArrayTypeFunction · 0.70
onErrorMethod · 0.65
attachedMethod · 0.45
sizeMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected