See More

/* * Copyright (c) 2016-present Samsung Electronics Co., Ltd * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA */ #include "Escargot.h" #include "Script.h" #include "interpreter/ByteCode.h" #include "interpreter/ByteCodeGenerator.h" #include "interpreter/ByteCodeInterpreter.h" #include "parser/ast/Node.h" #include "runtime/Context.h" #include "runtime/Global.h" #include "runtime/Environment.h" #include "runtime/EnvironmentRecord.h" #include "runtime/ErrorObject.h" #include "runtime/ExtendedNativeFunctionObject.h" #include "runtime/JSON.h" #include "runtime/SandBox.h" #include "runtime/ScriptFunctionObject.h" #include "runtime/ScriptAsyncFunctionObject.h" #include "runtime/ModuleNamespaceObject.h" #include "parser/ast/AST.h" namespace Escargot { void* Script::operator new(size_t size) { static MAY_THREAD_LOCAL bool typeInited = false; static MAY_THREAD_LOCAL GC_descr descr; if (!typeInited) { GC_word obj_bitmap[GC_BITMAP_SIZE(Script)] = { 0 }; GC_set_bit(obj_bitmap, GC_WORD_OFFSET(Script, m_srcName)); GC_set_bit(obj_bitmap, GC_WORD_OFFSET(Script, m_sourceCode)); GC_set_bit(obj_bitmap, GC_WORD_OFFSET(Script, m_topCodeBlock)); GC_set_bit(obj_bitmap, GC_WORD_OFFSET(Script, m_moduleData)); descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(Script)); typeInited = true; } return GC_MALLOC_EXPLICITLY_TYPED(size, descr); } bool Script::isExecuted() { if (isModule()) { return m_moduleData->m_status >= ModuleData::ModuleStatus::Evaluating; } return m_topCodeBlock->byteCodeBlock() == nullptr; } bool Script::wasThereErrorOnModuleEvaluation() { return m_moduleData && m_moduleData->m_evaluationError.hasValue(); } Value Script::moduleEvaluationError() { if (m_moduleData && m_moduleData->m_evaluationError.hasValue()) { return m_moduleData->m_evaluationError.value(); } return Value(); } Context* Script::context() { return m_topCodeBlock->context(); } Script* Script::loadModuleFromScript(ExecutionState& state, ModuleRequest& request) { Platform::LoadModuleResult result = Global::platform()->onLoadModule(context(), this, request.m_specifier, request.m_type); if (!result.script) { ErrorObject::throwBuiltinError(state, (ErrorCode)result.errorCode, result.errorMessage->toNonGCUTF8StringData().data()); return nullptr; } if (!result.script->moduleData()->m_didCallLoadedCallback) { Global::platform()->didLoadModule(context(), this, result.script.value()); result.script->moduleData()->m_didCallLoadedCallback = true; } return result.script.value(); } size_t Script::moduleRequestsLength() { if (!isModule()) { return 0; } return m_moduleData->m_requestedModules.size(); } String* Script::moduleRequest(size_t i) { ASSERT(isModule()); return m_moduleData->m_requestedModules[i].m_specifier; } Value Script::moduleInstantiate(ExecutionState& state) { ASSERT(isModule()); if (!moduleData()->m_didCallLoadedCallback) { Global::platform()->didLoadModule(context(), nullptr, this); moduleData()->m_didCallLoadedCallback = true; } auto result = moduleLinking(state); if (result.gotException) { throw result.value; } return result.value; } Value Script::moduleEvaluate(ExecutionState& state) { ASSERT(isModule()); auto result = moduleEvaluation(state); if (result.gotException) { throw result.value; } return result.value; } AtomicStringVector Script::exportedNames(ExecutionState& state, std::vector