• 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 "jerryscript_native_array_buffer.h"
17 
JerryScriptNativeArrayBuffer(JerryScriptNativeEngine * engine,jerry_value_t value)18 JerryScriptNativeArrayBuffer::JerryScriptNativeArrayBuffer(JerryScriptNativeEngine* engine, jerry_value_t value)
19     : JerryScriptNativeObject(engine, value)
20 {
21 }
22 
JerryScriptNativeArrayBuffer(JerryScriptNativeEngine * engine,void ** value,size_t length)23 JerryScriptNativeArrayBuffer::JerryScriptNativeArrayBuffer(JerryScriptNativeEngine* engine, void** value, size_t length)
24     : JerryScriptNativeArrayBuffer(engine, jerry_create_arraybuffer(length))
25 {
26     *value = jerry_get_arraybuffer_pointer(value_);
27 }
28 
JerryScriptNativeArrayBuffer(JerryScriptNativeEngine * engine,void * value,size_t length,NativeFinalize callback,void * hint)29 JerryScriptNativeArrayBuffer::JerryScriptNativeArrayBuffer(JerryScriptNativeEngine* engine,
30                                                            void* value,
31                                                            size_t length,
32                                                            NativeFinalize callback,
33                                                            void* hint)
34     : JerryScriptNativeArrayBuffer(engine, 0)
35 {
36     value_ = jerry_create_arraybuffer_external(length, (uint8_t*)value, [](void* nativePoint) { free(nativePoint); });
37 }
38 
~JerryScriptNativeArrayBuffer()39 JerryScriptNativeArrayBuffer::~JerryScriptNativeArrayBuffer() {}
40 
GetInterface(int interfaceId)41 void* JerryScriptNativeArrayBuffer::GetInterface(int interfaceId)
42 {
43     return (NativeArrayBuffer::INTERFACE_ID == interfaceId) ? (NativeArrayBuffer*)this
44                                                             : JerryScriptNativeObject::GetInterface(interfaceId);
45 }
46 
GetBuffer()47 void* JerryScriptNativeArrayBuffer::GetBuffer()
48 {
49     return jerry_get_arraybuffer_pointer(value_);
50 }
51 
GetLength()52 size_t JerryScriptNativeArrayBuffer::GetLength()
53 {
54     return jerry_get_arraybuffer_byte_length(value_);
55 }
56 
IsDetachedArrayBuffer()57 bool JerryScriptNativeArrayBuffer::IsDetachedArrayBuffer()
58 {
59     jerry_value_t res = jerry_is_arraybuffer_detachable(value_);
60     bool is_detachable = jerry_get_boolean_value(res);
61     jerry_release_value(res);
62     return !is_detachable;
63 }
64 
DetachArrayBuffer()65 bool JerryScriptNativeArrayBuffer::DetachArrayBuffer()
66 {
67     if (!IsDetachedArrayBuffer()) {
68         jerry_detach_arraybuffer(value_);
69         return true;
70     } else {
71         return false;
72     }
73 }
74