1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "quickjs_native_data_view.h"
17 #include "native_engine/native_engine.h"
18 #include "quickjs_headers.h"
19 #include "quickjs_native_array_buffer.h"
20
QuickJSNativeDataView(QuickJSNativeEngine * engine,JSValue value)21 QuickJSNativeDataView::QuickJSNativeDataView(QuickJSNativeEngine* engine, JSValue value)
22 : QuickJSNativeObject(engine, value)
23 {
24 }
25
QuickJSNativeDataView(QuickJSNativeEngine * engine,NativeValue * value,size_t length,size_t offset)26 QuickJSNativeDataView::QuickJSNativeDataView(QuickJSNativeEngine* engine,
27 NativeValue* value,
28 size_t length,
29 size_t offset)
30 : QuickJSNativeDataView(engine, JS_NULL)
31 {
32 JSValue global = JS_GetGlobalObject(engine_->GetContext());
33 JSValue dataView = JS_GetPropertyStr(engine_->GetContext(), global, "DataView");
34
35 JSValue param[] = {
36 *value,
37 JS_NewInt64(engine_->GetContext(), offset),
38 JS_NewInt64(engine_->GetContext(), length),
39 };
40
41 value_ = JS_CallConstructor(engine_->GetContext(), dataView, 3, param);
42
43 JS_FreeValue(engine_->GetContext(), dataView);
44 JS_FreeValue(engine_->GetContext(), global);
45 }
46
~QuickJSNativeDataView()47 QuickJSNativeDataView::~QuickJSNativeDataView() {}
48
GetInterface(int interfaceId)49 void* QuickJSNativeDataView::GetInterface(int interfaceId)
50 {
51 return (NativeDataView::INTERFACE_ID == interfaceId) ? (NativeDataView*)this
52 : QuickJSNativeObject::GetInterface(interfaceId);
53 }
54
GetBuffer()55 void* QuickJSNativeDataView::GetBuffer()
56 {
57 void* buffer = nullptr;
58 size_t bufferSize = 0;
59 JSValue arrayBuffer = JS_GetPropertyStr(engine_->GetContext(), value_, "buffer");
60 buffer = JS_GetArrayBuffer(engine_->GetContext(), &bufferSize, arrayBuffer);
61 JS_FreeValue(engine_->GetContext(), arrayBuffer);
62 return buffer;
63 }
64
GetLength()65 size_t QuickJSNativeDataView::GetLength()
66 {
67 uint32_t length = 0;
68 JSValue byteLength = JS_GetPropertyStr(engine_->GetContext(), value_, "byteLength");
69 JS_ToUint32(engine_->GetContext(), &length, byteLength);
70 JS_FreeValue(engine_->GetContext(), byteLength);
71 return length;
72 }
73
GetArrayBuffer()74 NativeValue* QuickJSNativeDataView::GetArrayBuffer()
75 {
76 JSValue buffer = JS_GetPropertyStr(engine_->GetContext(), value_, "buffer");
77 return QuickJSNativeEngine::JSValueToNativeValue(engine_, buffer);
78 }
79
GetOffset()80 size_t QuickJSNativeDataView::GetOffset()
81 {
82 JSValue byteOffset = JS_GetPropertyStr(engine_->GetContext(), value_, "byteOffset");
83 uint32_t cValue = 0;
84 JS_ToUint32(engine_->GetContext(), &cValue, byteOffset);
85 JS_FreeValue(engine_->GetContext(), byteOffset);
86 return cValue;
87 }
88