• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_array_buffer.h"
17 
18 struct V8NativeArrayBufferInfo {
CreateNewInstanceV8NativeArrayBufferInfo19     static V8NativeArrayBufferInfo* CreateNewInstance() { return new V8NativeArrayBufferInfo(); }
20     NativeEngine* engine = nullptr;
21     NativeFinalize cb = nullptr;
22     void* hint = nullptr;
23 };
24 
V8NativeArrayBuffer(V8NativeEngine * engine,v8::Local<v8::Value> value)25 V8NativeArrayBuffer::V8NativeArrayBuffer(V8NativeEngine* engine, v8::Local<v8::Value> value)
26     : V8NativeObject(engine, value)
27 {
28 }
29 
V8NativeArrayBuffer(V8NativeEngine * engine,uint8_t ** value,size_t length)30 V8NativeArrayBuffer::V8NativeArrayBuffer(V8NativeEngine* engine, uint8_t** value, size_t length)
31     : V8NativeArrayBuffer(engine, v8::Local<v8::Value>())
32 {
33     value_ = v8::ArrayBuffer::New(engine->GetIsolate(), length);
34     if (value != nullptr) {
35         v8::Local<v8::ArrayBuffer> obj = value_;
36         *value = (uint8_t*)obj->GetBackingStore()->Data();
37     }
38 }
39 
V8NativeArrayBuffer(V8NativeEngine * engine,uint8_t * value,size_t length,NativeFinalize cb,void * hint)40 V8NativeArrayBuffer::V8NativeArrayBuffer(V8NativeEngine* engine,
41                                          uint8_t* value,
42                                          size_t length,
43                                          NativeFinalize cb,
44                                          void* hint)
45     : V8NativeArrayBuffer(engine, v8::Local<v8::Value>())
46 {
47     auto cbinfo = V8NativeArrayBufferInfo::CreateNewInstance();
48     if (cbinfo != nullptr) {
49         cbinfo->engine = engine_;
50         cbinfo->cb = cb;
51         cbinfo->hint = hint;
52     }
53 
54     auto backingStore = v8::ArrayBuffer::NewBackingStore(
55         value, length,
56         [](void* data, size_t length, void* deleterData) -> void {
57             auto cbinfo = (V8NativeArrayBufferInfo*)deleterData;
58             if (cbinfo != nullptr) {
59                 cbinfo->cb(cbinfo->engine, data, cbinfo->hint);
60                 delete cbinfo;
61             }
62         },
63         cbinfo);
64 
65     value_ = v8::ArrayBuffer::New(engine->GetIsolate(), std::shared_ptr<v8::BackingStore>(backingStore.release()));
66 }
67 
~V8NativeArrayBuffer()68 V8NativeArrayBuffer::~V8NativeArrayBuffer() {}
69 
GetInterface(int interfaceId)70 void* V8NativeArrayBuffer::GetInterface(int interfaceId)
71 {
72     return (NativeArrayBuffer::INTERFACE_ID == interfaceId) ? (NativeArrayBuffer*)this
73                                                             : V8NativeObject::GetInterface(interfaceId);
74 }
75 
GetBuffer()76 void* V8NativeArrayBuffer::GetBuffer()
77 {
78     v8::Local<v8::ArrayBuffer> v = value_;
79     return v->GetBackingStore()->Data();
80 }
81 
GetLength()82 size_t V8NativeArrayBuffer::GetLength()
83 {
84     v8::Local<v8::ArrayBuffer> v = value_;
85     return v->ByteLength();
86 }
87