• 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 "ark_native_array_buffer.h"
17 #include "utils/log.h"
18 
19 using panda::ArrayBufferRef;
ArkNativeArrayBuffer(ArkNativeEngine * engine,Local<JSValueRef> value)20 ArkNativeArrayBuffer::ArkNativeArrayBuffer(ArkNativeEngine* engine, Local<JSValueRef> value)
21     : ArkNativeObject(engine, value)
22 {
23 }
24 
ArkNativeArrayBuffer(ArkNativeEngine * engine,uint8_t ** value,size_t length)25 ArkNativeArrayBuffer::ArkNativeArrayBuffer(ArkNativeEngine* engine, uint8_t** value, size_t length)
26     : ArkNativeArrayBuffer(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
27 {
28     auto vm = engine->GetEcmaVm();
29     LocalScope scope(vm);
30     value_ = Global<ArrayBufferRef>(vm, ArrayBufferRef::New(vm, length));
31     if (value != nullptr) {
32         Global<ArrayBufferRef> obj = value_;
33         *value = reinterpret_cast<uint8_t*>(obj->GetBuffer());
34     }
35 }
36 
ArkNativeArrayBuffer(ArkNativeEngine * engine,uint8_t * value,size_t length,NativeFinalize cb,void * hint)37 ArkNativeArrayBuffer::ArkNativeArrayBuffer(ArkNativeEngine* engine,
38                                            uint8_t* value,
39                                            size_t length,
40                                            NativeFinalize cb,
41                                            void* hint)
42     : ArkNativeArrayBuffer(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
43 {
44     auto vm = engine->GetEcmaVm();
45     LocalScope scope(vm);
46 
47     NativeObjectInfo* cbinfo = NativeObjectInfo::CreateNewInstance();
48     if (cbinfo == nullptr) {
49         HILOG_ERROR("cbinfo is nullptr");
50         return;
51     }
52     cbinfo->engine = engine_;
53     cbinfo->callback = cb;
54     cbinfo->hint = hint;
55 
56     Local<ArrayBufferRef> object = ArrayBufferRef::New(vm, value, length,
57         [](void* data, void* info) {
58             auto externalInfo = reinterpret_cast<NativeObjectInfo*>(info);
59             auto engine = externalInfo->engine;
60             auto callback = externalInfo->callback;
61             auto hint = externalInfo->hint;
62             if (callback != nullptr) {
63                 callback(engine, data, hint);
64             }
65             delete externalInfo;
66         },
67         cbinfo);
68 
69     value_ = Global<ArrayBufferRef>(vm, object);
70 }
71 
~ArkNativeArrayBuffer()72 ArkNativeArrayBuffer::~ArkNativeArrayBuffer() {}
73 
GetInterface(int interfaceId)74 void* ArkNativeArrayBuffer::GetInterface(int interfaceId)
75 {
76     return (NativeArrayBuffer::INTERFACE_ID == interfaceId) ? (NativeArrayBuffer*)this
77                                                             : ArkNativeObject::GetInterface(interfaceId);
78 }
79 
GetBuffer()80 void* ArkNativeArrayBuffer::GetBuffer()
81 {
82     auto vm = engine_->GetEcmaVm();
83     LocalScope scope(vm);
84     Global<ArrayBufferRef> v = value_;
85     return v->GetBuffer();
86 }
87 
GetLength()88 size_t ArkNativeArrayBuffer::GetLength()
89 {
90     auto vm = engine_->GetEcmaVm();
91     LocalScope scope(vm);
92     Global<ArrayBufferRef> v = value_;
93     return v->ByteLength(vm);
94 }
95 
IsDetachedArrayBuffer()96 bool ArkNativeArrayBuffer::IsDetachedArrayBuffer()
97 {
98     return true;
99 }
100 
DetachArrayBuffer()101 bool ArkNativeArrayBuffer::DetachArrayBuffer()
102 {
103     return false;
104 }
105