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
24 #include "securec.h"
25
26 namespace panda::ecmascript {
CopyDataBlockBytes(JSTaggedValue toBlock,JSTaggedValue fromBlock,int32_t fromIndex,int32_t count)27 void JSArrayBuffer::CopyDataBlockBytes(JSTaggedValue toBlock, JSTaggedValue fromBlock, int32_t fromIndex, int32_t count)
28 {
29 void *fromBuf = JSNativePointer::Cast(fromBlock.GetTaggedObject())->GetExternalPointer();
30 void *toBuf = JSNativePointer::Cast(toBlock.GetTaggedObject())->GetExternalPointer();
31 CopyDataPointBytes(toBuf, fromBuf, fromIndex, count);
32 }
33
CopyDataPointBytes(void * toBuf,void * fromBuf,int32_t fromIndex,int32_t count)34 void JSArrayBuffer::CopyDataPointBytes(void *toBuf, void *fromBuf, int32_t fromIndex, int32_t count)
35 {
36 auto *from = static_cast<uint8_t *>(fromBuf);
37 auto *to = static_cast<uint8_t *>(toBuf);
38 if (memcpy_s(to, count, from + fromIndex, count) != EOK) { // NOLINT
39 LOG_FULL(FATAL) << "memcpy_s failed";
40 UNREACHABLE();
41 }
42 }
43
Attach(JSThread * thread,uint32_t arrayBufferByteLength,JSTaggedValue arrayBufferData)44 void JSArrayBuffer::Attach(JSThread *thread, uint32_t arrayBufferByteLength, JSTaggedValue arrayBufferData)
45 {
46 ASSERT(arrayBufferData.IsNativePointer());
47 SetArrayBufferByteLength(arrayBufferByteLength);
48 SetArrayBufferData(thread, arrayBufferData);
49 }
50
Detach(JSThread * thread)51 void JSArrayBuffer::Detach(JSThread *thread)
52 {
53 JSTaggedValue arrayBufferData = GetArrayBufferData();
54 // already detached.
55 if (arrayBufferData.IsNull()) {
56 return;
57 }
58
59 SetArrayBufferData(thread, JSTaggedValue::Null());
60 SetArrayBufferByteLength(0);
61 }
62 } // namespace panda::ecmascript
63