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 "ark_native_data_view.h"
17 #include "ark_native_array_buffer.h"
18
19 using panda::DataViewRef;
20 using panda::ArrayBufferRef;
ArkNativeDataView(ArkNativeEngine * engine,Local<JSValueRef> value)21 ArkNativeDataView::ArkNativeDataView(ArkNativeEngine* engine, Local<JSValueRef> value) : ArkNativeObject(engine, value)
22 {
23 }
24
ArkNativeDataView(ArkNativeEngine * engine,NativeValue * value,size_t length,size_t offset)25 ArkNativeDataView::ArkNativeDataView(ArkNativeEngine* engine, NativeValue* value, size_t length, size_t offset)
26 : ArkNativeDataView(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
27 {
28 auto vm = engine->GetEcmaVm();
29 LocalScope scope(vm);
30 Global<ArrayBufferRef> arraybuffer = *value;
31 Local<DataViewRef> dataView = DataViewRef::New(vm, arraybuffer.ToLocal(vm), offset, length);
32 value_ = Global<DataViewRef>(vm, dataView);
33 }
34
~ArkNativeDataView()35 ArkNativeDataView::~ArkNativeDataView() {}
36
GetInterface(int interfaceId)37 void* ArkNativeDataView::GetInterface(int interfaceId)
38 {
39 return (NativeDataView::INTERFACE_ID == interfaceId) ? (NativeDataView*)this
40 : ArkNativeObject::GetInterface(interfaceId);
41 }
42
GetBuffer()43 void* ArkNativeDataView::GetBuffer()
44 {
45 auto vm = engine_->GetEcmaVm();
46 LocalScope scope(vm);
47 Global<DataViewRef> value = value_;
48
49 return value->GetArrayBuffer(vm)->GetBuffer();
50 }
51
GetLength()52 size_t ArkNativeDataView::GetLength()
53 {
54 auto vm = engine_->GetEcmaVm();
55 LocalScope scope(vm);
56 Global<DataViewRef> value = value_;
57
58 return value->ByteLength();
59 }
60
GetArrayBuffer()61 NativeValue* ArkNativeDataView::GetArrayBuffer()
62 {
63 auto vm = engine_->GetEcmaVm();
64 LocalScope scope(vm);
65 Global<DataViewRef> value = value_;
66
67 NativeChunk& chunk = engine_->GetNativeChunk();
68 return chunk.New<ArkNativeArrayBuffer>(engine_, value->GetArrayBuffer(vm));
69 }
70
GetOffset()71 size_t ArkNativeDataView::GetOffset()
72 {
73 auto vm = engine_->GetEcmaVm();
74 LocalScope scope(vm);
75 Global<DataViewRef> value = value_;
76
77 return value->ByteOffset();
78 }
79