/* * Tencent is pleased to support the open source community by making ScriptX available. * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "../../src/Exception.h" #include "../../src/Reference.h" #include "../../src/Scope.h" #include "../../src/Value.h" #include "WasmEngine.hpp" namespace script { Local Object::newObject() { return Local(wasm_backend::Stack::newObject()); } Local Object::newObjectImpl(const Local& type, size_t size, const Local* args) { auto base = wasm_backend::Stack::top() + 1; for (size_t i = 0; i < size; ++i) { wasm_backend::Stack::pushValue(args[i]); } return Local( wasm_backend::Stack::newObject(wasm_backend::WasmEngine::refIndex(type.asObject()), base)); } Local String::newString(const char* utf8) { return Local(wasm_backend::Stack::newString(utf8)); } Local String::newString(std::string_view utf8) { return Local(wasm_backend::Stack::newString(utf8.data(), utf8.length())); } Local String::newString(const std::string& utf8) { return Local(wasm_backend::Stack::newString(utf8.data(), utf8.length())); } #if defined(__cpp_char8_t) Local String::newString(const char8_t* utf8) { return newString(reinterpret_cast(utf8)); } Local String::newString(std::u8string_view utf8) { return newString(std::string_view(reinterpret_cast(utf8.data()), utf8.length())); } Local String::newString(const std::u8string& utf8) { return newString(std::string_view(reinterpret_cast(utf8.data()), utf8.length())); } #endif Local Number::newNumber(float value) { return Local(wasm_backend::Stack::newNumber(static_cast(value))); } Local Number::newNumber(double value) { return Local(wasm_backend::Stack::newNumber(value)); } Local Number::newNumber(int32_t value) { return Local(wasm_backend::Stack::newNumber(value)); } Local Number::newNumber(int64_t value) { return Local(wasm_backend::Stack::newNumber(static_cast(value))); } Local Boolean::newBoolean(bool value) { return Local(wasm_backend::Stack::newBoolean(value)); } Local Function::newFunction(script::FunctionCallback callback) { auto copy = std::make_unique(std::move(callback)); auto ret = wasm_backend::Stack::newFunction( [](const Arguments& args, void* ptr0, void*) -> Local { auto& callback = *static_cast(ptr0); return callback(args); }, copy.get()); copy.release(); return Local(ret); } Local Array::newArray(size_t size) { return Local(wasm_backend::Stack::newArray(size)); } Local Array::newArrayImpl(size_t size, const Local* args) { auto ret = newArray(size); { StackFrameScope stackFrame; for (size_t i = 0; i < size; ++i) { ret.set(i, args[i]); } } return ret; } Local ByteBuffer::newByteBuffer(size_t size) { return Local(wasm_backend::Stack::pushArrayBuffer(size)); } Local ByteBuffer::newByteBuffer(void* nativeBuffer, size_t size) { auto buffer = newByteBuffer(size); std::memcpy(buffer.getRawBytes(), nativeBuffer, size); buffer.commit(); return buffer; } Local ByteBuffer::newByteBuffer(std::shared_ptr nativeBuffer, size_t size) { return Local(wasm_backend::ByteBufferState(std::move(nativeBuffer), size)); } } // namespace script