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 "v8_native_data_view.h"
17
18 #include "v8_native_array_buffer.h"
19
V8NativeDataView(V8NativeEngine * engine,v8::Local<v8::Value> value)20 V8NativeDataView::V8NativeDataView(V8NativeEngine* engine, v8::Local<v8::Value> value) : V8NativeObject(engine, value)
21 {
22 }
23
V8NativeDataView(V8NativeEngine * engine,NativeValue * value,size_t length,size_t offset)24 V8NativeDataView::V8NativeDataView(V8NativeEngine* engine, NativeValue* value, size_t length, size_t offset)
25 : V8NativeDataView(engine, v8::Local<v8::DataView>())
26 {
27 v8::Local<v8::ArrayBuffer> arrybuffer = *value;
28 value_ = v8::DataView::New(arrybuffer, offset, length);
29 }
30
~V8NativeDataView()31 V8NativeDataView::~V8NativeDataView() {}
32
GetInterface(int interfaceId)33 void* V8NativeDataView::GetInterface(int interfaceId)
34 {
35 return (NativeDataView::INTERFACE_ID == interfaceId) ? (NativeDataView*)this
36 : V8NativeObject::GetInterface(interfaceId);
37 }
38
GetBuffer()39 void* V8NativeDataView::GetBuffer()
40 {
41 v8::Local<v8::DataView> value = value_;
42
43 return value->Buffer()->GetBackingStore()->Data();
44 }
45
GetLength()46 size_t V8NativeDataView::GetLength()
47 {
48 v8::Local<v8::DataView> value = value_;
49
50 return value->ByteLength();
51 }
52
GetArrayBuffer()53 NativeValue* V8NativeDataView::GetArrayBuffer()
54 {
55 v8::Local<v8::DataView> value = value_;
56
57 return new V8NativeArrayBuffer(engine_, value->Buffer());
58 }
59
GetOffset()60 size_t V8NativeDataView::GetOffset()
61 {
62 v8::Local<v8::DataView> value = value_;
63
64 return value->ByteOffset();
65 }
66