Skip to content

Commit

Permalink
Add ESCARGOT_USE_EXTENDED_API build option to managed APIs used only …
Browse files Browse the repository at this point in the history
…for third party

Signed-off-by: HyukWoo Park <hyukwoo.park@samsung.com>
  • Loading branch information
clover2123 authored and ksh8281 committed May 14, 2024
1 parent a34205a commit 77f0c49
Show file tree
Hide file tree
Showing 21 changed files with 106 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/es-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ jobs:
sudo apt-get install -y ninja-build gcc-multilib g++-multilib
- name: Build x86/x64
env:
BUILD_OPTIONS_X86: -DCMAKE_SYSTEM_PROCESSOR=x86 -DESCARGOT_MODE=debug -DESCARGOT_THREADING=ON -DESCARGOT_DEBUGGER=1 -DESCARGOT_TEST=ON -DESCARGOT_OUTPUT=cctest -GNinja
BUILD_OPTIONS_X64: -DESCARGOT_MODE=debug -DESCARGOT_THREADING=1 -DESCARGOT_DEBUGGER=1 -DESCARGOT_TEST=ON -DESCARGOT_OUTPUT=cctest -GNinja
BUILD_OPTIONS_X86: -DCMAKE_SYSTEM_PROCESSOR=x86 -DESCARGOT_MODE=debug -DESCARGOT_THREADING=ON -DESCARGOT_DEBUGGER=1 -DESCARGOT_USE_EXTENDED_API=ON -DESCARGOT_TEST=ON -DESCARGOT_OUTPUT=cctest -GNinja
BUILD_OPTIONS_X64: -DESCARGOT_MODE=debug -DESCARGOT_THREADING=1 -DESCARGOT_DEBUGGER=1 -DESCARGOT_USE_EXTENDED_API=ON -DESCARGOT_TEST=ON -DESCARGOT_OUTPUT=cctest -GNinja
run: |
cmake -H. -Bout/cctest/x86 $BUILD_OPTIONS_X86
ninja -Cout/cctest/x86
Expand Down
4 changes: 4 additions & 0 deletions build/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ IF (ESCARGOT_LIBICU_SUPPORT)
ENDIF()
ENDIF()

IF (ESCARGOT_USE_EXTENDED_API)
SET (ESCARGOT_DEFINITIONS ${ESCARGOT_DEFINITIONS} -DENABLE_EXTENDED_API)
ENDIF()

IF (ESCARGOT_USE_CUSTOM_LOGGING)
SET (ESCARGOT_DEFINITIONS ${ESCARGOT_DEFINITIONS} -DENABLE_CUSTOM_LOGGING)
ELSEIF (${ESCARGOT_HOST} STREQUAL "tizen" OR ${ESCARGOT_HOST} STREQUAL "tizen_obs")
Expand Down
12 changes: 6 additions & 6 deletions src/api/EscargotPublic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,7 @@ void VMInstanceRef::setOnVMInstanceDelete(OnVMInstanceDelete cb)
(void*)cb);
}

#if defined(ENABLE_EXTENDED_API)
void VMInstanceRef::registerErrorCreationCallback(ErrorCallback cb)
{
toImpl(this)->registerErrorCreationCallback([](ExecutionState& state, ErrorObject* err, void* cb) -> void {
Expand Down Expand Up @@ -1165,6 +1166,7 @@ void VMInstanceRef::unregisterErrorThrowCallback()
{
toImpl(this)->unregisterErrorThrowCallback();
}
#endif

void VMInstanceRef::registerPromiseHook(PromiseHook promiseHook)
{
Expand Down Expand Up @@ -3234,7 +3236,7 @@ GlobalObjectRef* ExecutionStateRef::resolveCallerLexicalGlobalObject()
return toRef(ctx->globalObject());
}

#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
bool ExecutionStateRef::onTry()
{
return toImpl(this)->onTry();
Expand All @@ -3249,11 +3251,6 @@ bool ExecutionStateRef::onFinally()
{
return toImpl(this)->onFinally();
}
#else
// these three functions are used only for test purpose
bool ExecutionStateRef::onTry() { return false; }
bool ExecutionStateRef::onCatch() { return false; }
bool ExecutionStateRef::onFinally() { return false; }
#endif

void ExecutionStateRef::throwException(ValueRef* value)
Expand Down Expand Up @@ -4412,6 +4409,8 @@ FinalizationRegistryObjectRef* FinalizationRegistryObjectRef::create(ExecutionSt
return toRef(new FinalizationRegistryObject(*toImpl(state), toImpl(cleanupCallback), toImpl(realm)));
}


#if defined(ENABLE_EXTENDED_API)
void TemplateRef::set(ValueRef* propertyName, ValueRef* data, bool isWritable, bool isEnumerable, bool isConfigurable)
{
toImpl(this)->set(toImpl(propertyName), toImpl(data), isWritable, isEnumerable, isConfigurable);
Expand Down Expand Up @@ -4605,6 +4604,7 @@ OptionalRef<FunctionTemplateRef> FunctionTemplateRef::parent()
return nullptr;
}
}
#endif

ScriptParserRef::InitializeScriptResult::InitializeScriptResult()
: script()
Expand Down
21 changes: 13 additions & 8 deletions src/api/EscargotPublic.h
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ class ESCARGOT_EXPORT ExecutionStateRef {
GCManagedVector<FunctionObjectRef*> resolveCallstack(); // resolve list of callee
GlobalObjectRef* resolveCallerLexicalGlobalObject(); // resolve caller's lexical global object

// these 3 functions are used only for test purpose
// only enabled when `ENABLE_EXTENDED_API` macro is set (default: disabled)
bool onTry();
bool onCatch();
bool onFinally();
Expand All @@ -662,6 +662,8 @@ class ESCARGOT_EXPORT VMInstanceRef {

// register ErrorCallback which is triggered when each Error constructor (e.g. new TypeError()) invoked or thrown
// parameter `err` stands for the newly created ErrorObject
// these functions are used only for third party usage
// only enabled when `ENABLE_EXTENDED_API` macro is set (default: disabled)
typedef void (*ErrorCallback)(ExecutionStateRef* state, ErrorObjectRef* err);
void registerErrorCreationCallback(ErrorCallback cb);
void registerErrorThrowCallback(ErrorCallback cb);
Expand Down Expand Up @@ -1956,6 +1958,7 @@ class ESCARGOT_EXPORT FinalizationRegistryObjectRef : public ObjectRef {
// it is not intented operation
// Note) only String or Symbol type is allowed for `propertyName`
// because TemplateRef is set without ExecutionStateRef, so property name conversion is impossible.
// only enabled when `ENABLE_EXTENDED_API` macro is set (default: disabled)
class ESCARGOT_EXPORT TemplateRef {
public:
void set(ValueRef* propertyName, ValueRef* data, bool isWritable, bool isEnumerable, bool isConfigurable);
Expand Down Expand Up @@ -2039,13 +2042,7 @@ struct ESCARGOT_EXPORT ObjectTemplatePropertyHandlerConfiguration {
}
};

class ESCARGOT_EXPORT SerializerRef {
public:
// returns the serialization was successful
static bool serializeInto(ValueRef* value, std::ostringstream& output);
static ValueRef* deserializeFrom(ContextRef* context, std::istringstream& input);
};

// only enabled when `ENABLE_EXTENDED_API` macro is set (default: disabled)
class ESCARGOT_EXPORT ObjectTemplateRef : public TemplateRef {
public:
static ObjectTemplateRef* create();
Expand All @@ -2062,6 +2059,7 @@ class ESCARGOT_EXPORT ObjectTemplateRef : public TemplateRef {
};

// FunctionTemplateRef returns the unique function instance in context.
// only enabled when `ENABLE_EXTENDED_API` macro is set (default: disabled)
class ESCARGOT_EXPORT FunctionTemplateRef : public TemplateRef {
public:
// in constructor call, thisValue is default consturcted object
Expand All @@ -2084,6 +2082,13 @@ class ESCARGOT_EXPORT FunctionTemplateRef : public TemplateRef {
OptionalRef<FunctionTemplateRef> parent();
};

class ESCARGOT_EXPORT SerializerRef {
public:
// returns the serialization was successful
static bool serializeInto(ValueRef* value, std::ostringstream& output);
static ValueRef* deserializeFrom(ContextRef* context, std::istringstream& input);
};

class ESCARGOT_EXPORT ScriptParserRef {
public:
struct ESCARGOT_EXPORT InitializeScriptResult {
Expand Down
26 changes: 26 additions & 0 deletions src/builtins/BuiltinError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ static Value builtinErrorConstructor(ExecutionState& state, Value thisValue, siz
Value options = argc > 1 ? argv[1] : Value();
installErrorCause(state, obj, options);

#if defined(ENABLE_EXTENDED_API)
if (UNLIKELY(state.context()->vmInstance()->isErrorCreationCallbackRegistered())) {
state.context()->vmInstance()->triggerErrorCreationCallback(state, obj);
}
#endif

return obj;
}

#if defined(ENABLE_EXTENDED_API)
#define DEFINE_ERROR_CTOR(errorName, lowerCaseErrorName) \
static Value builtin##errorName##ErrorConstructor(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget) \
{ \
Expand All @@ -92,6 +95,27 @@ static Value builtinErrorConstructor(ExecutionState& state, Value thisValue, siz
} \
return obj; \
}
#else
#define DEFINE_ERROR_CTOR(errorName, lowerCaseErrorName) \
static Value builtin##errorName##ErrorConstructor(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget) \
{ \
if (!newTarget.hasValue()) { \
newTarget = state.resolveCallee(); \
} \
Object* proto = Object::getPrototypeFromConstructor(state, newTarget.value(), [](ExecutionState& state, Context* constructorRealm) -> Object* { \
return constructorRealm->globalObject()->lowerCaseErrorName##ErrorPrototype(); \
}); \
ErrorObject* obj = new errorName##ErrorObject(state, proto, String::emptyString); \
Value message = argv[0]; \
if (!message.isUndefined()) { \
obj->defineOwnPropertyThrowsException(state, state.context()->staticStrings().message, \
ObjectPropertyDescriptor(message.toString(state), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectStructurePropertyDescriptor::ConfigurablePresent))); \
} \
Value options = argc > 1 ? argv[1] : Value(); \
installErrorCause(state, obj, options); \
return obj; \
}
#endif

DEFINE_ERROR_CTOR(Reference, reference);
DEFINE_ERROR_CTOR(Type, type);
Expand Down Expand Up @@ -131,9 +155,11 @@ static Value builtinAggregateErrorConstructor(ExecutionState& state, Value thisV
O->defineOwnPropertyThrowsException(state, ObjectPropertyName(state, String::fromASCII("errors")),
ObjectPropertyDescriptor(Value(Object::createArrayFromList(state, errorsList.size(), errorsList.data())), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectStructurePropertyDescriptor::ConfigurablePresent)));

#if defined(ENABLE_EXTENDED_API)
if (UNLIKELY(state.context()->vmInstance()->isErrorCreationCallbackRegistered())) {
state.context()->vmInstance()->triggerErrorCreationCallback(state, O);
}
#endif

// Return O.
return O;
Expand Down
12 changes: 6 additions & 6 deletions src/interpreter/ByteCodeInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ ALWAYS_INLINE size_t jumpTo(uint8_t* codeBuffer, const size_t jumpPosition)
return (size_t)&codeBuffer[jumpPosition];
}

#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
template <typename T>
class ExecutionStateVariableChanger {
public:
Expand Down Expand Up @@ -3294,7 +3294,7 @@ NEVER_INLINE Value InterpreterSlowPath::tryOperation(ExecutionState*& state, siz

if (LIKELY(!code->m_isCatchResumeProcess && !code->m_isFinallyResumeProcess)) {
try {
#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
ExecutionStateVariableChanger<void (*)(ExecutionState&, bool)> changer(*state, [](ExecutionState& state, bool in) {
state.m_onTry = in;
});
Expand Down Expand Up @@ -3350,7 +3350,7 @@ NEVER_INLINE Value InterpreterSlowPath::tryOperation(ExecutionState*& state, siz
stackTraceDataVector.clear();
registerFile[code->m_catchedValueRegisterIndex] = val;
try {
#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
ExecutionStateVariableChanger<void (*)(ExecutionState&, bool)> changer(*state, [](ExecutionState& state, bool in) {
state.m_onCatch = in;
});
Expand All @@ -3367,7 +3367,7 @@ NEVER_INLINE Value InterpreterSlowPath::tryOperation(ExecutionState*& state, siz
}
} else if (code->m_isCatchResumeProcess) {
try {
#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
ExecutionStateVariableChanger<void (*)(ExecutionState&, bool)> changer(*state, [](ExecutionState& state, bool in) {
state.m_onCatch = in;
});
Expand All @@ -3388,7 +3388,7 @@ NEVER_INLINE Value InterpreterSlowPath::tryOperation(ExecutionState*& state, siz
}

if (code->m_isFinallyResumeProcess) {
#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
ExecutionStateVariableChanger<void (*)(ExecutionState&, bool)> changer(*state, [](ExecutionState& state, bool in) {
state.m_onFinally = in;
});
Expand All @@ -3411,7 +3411,7 @@ NEVER_INLINE Value InterpreterSlowPath::tryOperation(ExecutionState*& state, siz
newState = new ExtendedExecutionState(state, state->lexicalEnvironment(), state->inStrictMode());
newState->rareData()->setControlFlowRecordVector(state->rareData()->controlFlowRecordVector());
}
#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
ExecutionStateVariableChanger<void (*)(ExecutionState&, bool)> changer(*state, [](ExecutionState& state, bool in) {
state.m_onFinally = in;
});
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ void Context::throwException(ExecutionState& state, const Value& exception)
{
if (LIKELY(vmInstance()->currentSandBox() != nullptr)) {
ASSERT(!!m_instance);
#if defined(ENABLE_EXTENDED_API)
if (UNLIKELY(m_instance->isErrorThrowCallbackRegistered() && exception.isObject() && exception.asObject()->isErrorObject())) {
// trigger ErrorThrowCallback when an ErrorObject is thrown
m_instance->triggerErrorThrowCallback(state, exception.asObject()->asErrorObject());
}
#endif
vmInstance()->currentSandBox()->throwException(state, exception);
} else {
ESCARGOT_LOG_ERROR("there is no sandbox but exception occurred");
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/ErrorObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static Value builtinErrorObjectStackInfoSet(ExecutionState& state, Value thisVal
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, "set Error.prototype.stack called on incompatible receiver");
}

// Do noting
// Do nothing
return Value();
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/ExecutionState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ExecutionState::ExecutionState()
, m_inStrictMode(false)
, m_isNativeFunctionObjectExecutionContext(false)
, m_inExecutionStopState(false)
#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
, m_onTry(false)
, m_onCatch(false)
, m_onFinally(false)
Expand Down
12 changes: 6 additions & 6 deletions src/runtime/ExecutionState.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class ExecutionState : public gc {
, m_inStrictMode(inStrictMode)
, m_isNativeFunctionObjectExecutionContext(false)
, m_inExecutionStopState(false)
#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
, m_onTry(false)
, m_onCatch(false)
, m_onFinally(false)
Expand All @@ -113,7 +113,7 @@ class ExecutionState : public gc {
, m_inStrictMode(false)
, m_isNativeFunctionObjectExecutionContext(false)
, m_inExecutionStopState(false)
#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
, m_onTry(false)
, m_onCatch(false)
, m_onFinally(false)
Expand All @@ -135,7 +135,7 @@ class ExecutionState : public gc {
, m_inStrictMode(inStrictMode)
, m_isNativeFunctionObjectExecutionContext(false)
, m_inExecutionStopState(false)
#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
, m_onTry(false)
, m_onCatch(false)
, m_onFinally(false)
Expand All @@ -157,7 +157,7 @@ class ExecutionState : public gc {
, m_inStrictMode(inStrictMode)
, m_isNativeFunctionObjectExecutionContext(true)
, m_inExecutionStopState(false)
#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
, m_onTry(false)
, m_onCatch(false)
, m_onFinally(false)
Expand Down Expand Up @@ -230,7 +230,7 @@ class ExecutionState : public gc {
return m_inExecutionStopState;
}

#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
bool onTry() const
{
return m_onTry;
Expand Down Expand Up @@ -332,7 +332,7 @@ class ExecutionState : public gc {
bool m_inStrictMode : 1;
bool m_isNativeFunctionObjectExecutionContext : 1;
bool m_inExecutionStopState : 1;
#if defined(ESCARGOT_ENABLE_TEST)
#if defined(ENABLE_EXTENDED_API)
bool m_onTry : 1;
bool m_onCatch : 1;
bool m_onFinally : 1;
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/FunctionTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* USA
*/

#if defined(ENABLE_EXTENDED_API)

#include "Escargot.h"
#include "FunctionTemplate.h"
#include "runtime/ObjectTemplate.h"
Expand Down Expand Up @@ -235,3 +237,5 @@ Object* FunctionTemplate::instantiate(Context* ctx)
return result;
}
} // namespace Escargot

#endif // ENABLE_EXTENDED_API
4 changes: 4 additions & 0 deletions src/runtime/FunctionTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#ifndef __EscargotFunctionTemplate__
#define __EscargotFunctionTemplate__

#if defined(ENABLE_EXTENDED_API)

#include "runtime/Template.h"
#include "api/EscargotPublic.h"

Expand Down Expand Up @@ -94,4 +96,6 @@ class FunctionTemplate : public Template {
};
} // namespace Escargot

#endif // ENABLE_EXTENDED_API

#endif
Loading

0 comments on commit 77f0c49

Please sign in to comment.