• 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 "ecmascript/js_arraybuffer.h"
17 
18 #include "ecmascript/base/builtins_base.h"
19 #include "ecmascript/ecma_macros.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/object_factory.h"
22 #include "ecmascript/tagged_array.h"
23 #include "securec.h"
24 
25 namespace panda::ecmascript {
CopyDataBlockBytes(JSTaggedValue toBlock,JSTaggedValue fromBlock,int32_t fromIndex,int32_t count)26 void JSArrayBuffer::CopyDataBlockBytes(JSTaggedValue toBlock, JSTaggedValue fromBlock, int32_t fromIndex, int32_t count)
27 {
28     void *fromBuf = JSNativePointer::Cast(fromBlock.GetTaggedObject())->GetExternalPointer();
29     void *toBuf = JSNativePointer::Cast(toBlock.GetTaggedObject())->GetExternalPointer();
30     auto *from = static_cast<uint8_t *>(fromBuf);
31     auto *to = static_cast<uint8_t *>(toBuf);
32     if (memcpy_s(to, count, from + fromIndex, count) != EOK) {  // NOLINT
33         LOG_ECMA(FATAL) << "memcpy_s failed";
34         UNREACHABLE();
35     }
36 }
37 
Attach(JSThread * thread,uint32_t arrayBufferByteLength,JSTaggedValue arrayBufferData)38 void JSArrayBuffer::Attach(JSThread *thread, uint32_t arrayBufferByteLength, JSTaggedValue arrayBufferData)
39 {
40     ASSERT(arrayBufferData.IsNativePointer());
41     SetArrayBufferByteLength(arrayBufferByteLength);
42     SetArrayBufferData(thread, arrayBufferData);
43     EcmaVM *vm = thread->GetEcmaVM();
44     vm->PushToArrayDataList(JSNativePointer::Cast(arrayBufferData.GetHeapObject()));
45 }
46 
Detach(JSThread * thread)47 void JSArrayBuffer::Detach(JSThread *thread)
48 {
49     JSTaggedValue arrayBufferData = GetArrayBufferData();
50     // already detached.
51     if (arrayBufferData.IsNull()) {
52         return;
53     }
54 
55     EcmaVM *vm = thread->GetEcmaVM();
56     // remove vm's control over arrayBufferData.
57     JSNativePointer *jsNativePointer = JSNativePointer::Cast(arrayBufferData.GetHeapObject());
58     vm->RemoveArrayDataList(jsNativePointer);
59     jsNativePointer->Destroy();
60 
61     SetArrayBufferData(thread, JSTaggedValue::Null());
62     SetArrayBufferByteLength(0);
63 }
64 }  // namespace panda::ecmascript
65