• 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 "native_engine/native_engine.h"
17 #include "quickjs_native_array_buffer.h"
18 
19 struct QuickJsArrayCallback {
CreateNewInstanceQuickJsArrayCallback20     static QuickJsArrayCallback* CreateNewInstance() { return new QuickJsArrayCallback(); }
21     NativeEngine* engine = nullptr;
22     NativeFinalize cb = nullptr;
23     void* hint = nullptr;
24 };
25 
QuickJSNativeArrayBuffer(QuickJSNativeEngine * engine,JSValue value)26 QuickJSNativeArrayBuffer::QuickJSNativeArrayBuffer(QuickJSNativeEngine* engine, JSValue value)
27     : QuickJSNativeObject(engine, value)
28 {
29 }
30 
QuickJSNativeArrayBuffer(QuickJSNativeEngine * engine,uint8_t ** data,size_t length)31 QuickJSNativeArrayBuffer::QuickJSNativeArrayBuffer(QuickJSNativeEngine* engine, uint8_t** data, size_t length)
32     : QuickJSNativeArrayBuffer(engine, JS_NULL)
33 {
34     size_t size = 0;
35     value_ = JS_NewArrayBufferCopy(engine_->GetContext(), nullptr, length);
36     *data = JS_GetArrayBuffer(engine_->GetContext(), &size, value_);
37 }
38 
QuickJSNativeArrayBuffer(QuickJSNativeEngine * engine,uint8_t * data,size_t length,NativeFinalize cb,void * hint)39 QuickJSNativeArrayBuffer::QuickJSNativeArrayBuffer(QuickJSNativeEngine* engine,
40                                                    uint8_t* data,
41                                                    size_t length,
42                                                    NativeFinalize cb,
43                                                    void* hint)
44     : QuickJSNativeObject(engine, JS_NULL)
45 {
46     auto cbinfo = QuickJsArrayCallback::CreateNewInstance();
47     if (cbinfo != nullptr) {
48         cbinfo->engine = engine_;
49         cbinfo->cb = cb;
50         cbinfo->hint = hint;
51     }
52     value_ = JS_NewArrayBuffer(
53         engine_->GetContext(), data, length,
54         [](JSRuntime* rt, void* opaque, void* ptr) -> void {
55             auto cbinfo = reinterpret_cast<QuickJsArrayCallback*>(opaque);
56             if (cbinfo != nullptr) {
57                 cbinfo->cb(cbinfo->engine, ptr, cbinfo->hint);
58                 delete cbinfo;
59             }
60         },
61         (void*)cbinfo, false);
62 }
63 
~QuickJSNativeArrayBuffer()64 QuickJSNativeArrayBuffer::~QuickJSNativeArrayBuffer() {}
65 
GetInterface(int interfaceId)66 void* QuickJSNativeArrayBuffer::GetInterface(int interfaceId)
67 {
68     return (NativeArrayBuffer::INTERFACE_ID == interfaceId) ? (NativeArrayBuffer*)this
69                                                             : QuickJSNativeObject::GetInterface(interfaceId);
70 }
71 
GetBuffer()72 void* QuickJSNativeArrayBuffer::GetBuffer()
73 {
74     void* buffer = nullptr;
75     size_t bufferSize = 0;
76     buffer = JS_GetArrayBuffer(engine_->GetContext(), &bufferSize, value_);
77     return buffer;
78 }
79 
GetLength()80 size_t QuickJSNativeArrayBuffer::GetLength()
81 {
82     size_t bufferSize = 0;
83     JS_GetArrayBuffer(engine_->GetContext(), &bufferSize, value_);
84     return bufferSize;
85 }
86 
IsDetachedArrayBuffer()87 bool QuickJSNativeArrayBuffer::IsDetachedArrayBuffer()
88 {
89     void* buffer = nullptr;
90     size_t bufferSize = 0;
91     buffer = JS_GetArrayBuffer(engine_->GetContext(), &bufferSize, value_);
92     return (buffer == nullptr);
93 }
94 
DetachArrayBuffer()95 bool QuickJSNativeArrayBuffer::DetachArrayBuffer()
96 {
97     if (!IsDetachedArrayBuffer()) {
98         JS_DetachArrayBuffer(engine_->GetContext(), value_);
99         return true;
100     } else {
101         return false;
102     }
103 }
104