Skip to content

Commit 77f0c49

Browse files
clover2123ksh8281
authored andcommitted
Add ESCARGOT_USE_EXTENDED_API build option to managed APIs used only for third party
Signed-off-by: HyukWoo Park <[email protected]>
1 parent a34205a commit 77f0c49

21 files changed

+106
-37
lines changed

.github/workflows/es-actions.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ jobs:
446446
sudo apt-get install -y ninja-build gcc-multilib g++-multilib
447447
- name: Build x86/x64
448448
env:
449-
BUILD_OPTIONS_X86: -DCMAKE_SYSTEM_PROCESSOR=x86 -DESCARGOT_MODE=debug -DESCARGOT_THREADING=ON -DESCARGOT_DEBUGGER=1 -DESCARGOT_TEST=ON -DESCARGOT_OUTPUT=cctest -GNinja
450-
BUILD_OPTIONS_X64: -DESCARGOT_MODE=debug -DESCARGOT_THREADING=1 -DESCARGOT_DEBUGGER=1 -DESCARGOT_TEST=ON -DESCARGOT_OUTPUT=cctest -GNinja
449+
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
450+
BUILD_OPTIONS_X64: -DESCARGOT_MODE=debug -DESCARGOT_THREADING=1 -DESCARGOT_DEBUGGER=1 -DESCARGOT_USE_EXTENDED_API=ON -DESCARGOT_TEST=ON -DESCARGOT_OUTPUT=cctest -GNinja
451451
run: |
452452
cmake -H. -Bout/cctest/x86 $BUILD_OPTIONS_X86
453453
ninja -Cout/cctest/x86

build/config.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ IF (ESCARGOT_LIBICU_SUPPORT)
112112
ENDIF()
113113
ENDIF()
114114

115+
IF (ESCARGOT_USE_EXTENDED_API)
116+
SET (ESCARGOT_DEFINITIONS ${ESCARGOT_DEFINITIONS} -DENABLE_EXTENDED_API)
117+
ENDIF()
118+
115119
IF (ESCARGOT_USE_CUSTOM_LOGGING)
116120
SET (ESCARGOT_DEFINITIONS ${ESCARGOT_DEFINITIONS} -DENABLE_CUSTOM_LOGGING)
117121
ELSEIF (${ESCARGOT_HOST} STREQUAL "tizen" OR ${ESCARGOT_HOST} STREQUAL "tizen_obs")

src/api/EscargotPublic.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,7 @@ void VMInstanceRef::setOnVMInstanceDelete(OnVMInstanceDelete cb)
11381138
(void*)cb);
11391139
}
11401140

1141+
#if defined(ENABLE_EXTENDED_API)
11411142
void VMInstanceRef::registerErrorCreationCallback(ErrorCallback cb)
11421143
{
11431144
toImpl(this)->registerErrorCreationCallback([](ExecutionState& state, ErrorObject* err, void* cb) -> void {
@@ -1165,6 +1166,7 @@ void VMInstanceRef::unregisterErrorThrowCallback()
11651166
{
11661167
toImpl(this)->unregisterErrorThrowCallback();
11671168
}
1169+
#endif
11681170

11691171
void VMInstanceRef::registerPromiseHook(PromiseHook promiseHook)
11701172
{
@@ -3234,7 +3236,7 @@ GlobalObjectRef* ExecutionStateRef::resolveCallerLexicalGlobalObject()
32343236
return toRef(ctx->globalObject());
32353237
}
32363238

3237-
#if defined(ESCARGOT_ENABLE_TEST)
3239+
#if defined(ENABLE_EXTENDED_API)
32383240
bool ExecutionStateRef::onTry()
32393241
{
32403242
return toImpl(this)->onTry();
@@ -3249,11 +3251,6 @@ bool ExecutionStateRef::onFinally()
32493251
{
32503252
return toImpl(this)->onFinally();
32513253
}
3252-
#else
3253-
// these three functions are used only for test purpose
3254-
bool ExecutionStateRef::onTry() { return false; }
3255-
bool ExecutionStateRef::onCatch() { return false; }
3256-
bool ExecutionStateRef::onFinally() { return false; }
32573254
#endif
32583255

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

4412+
4413+
#if defined(ENABLE_EXTENDED_API)
44154414
void TemplateRef::set(ValueRef* propertyName, ValueRef* data, bool isWritable, bool isEnumerable, bool isConfigurable)
44164415
{
44174416
toImpl(this)->set(toImpl(propertyName), toImpl(data), isWritable, isEnumerable, isConfigurable);
@@ -4605,6 +4604,7 @@ OptionalRef<FunctionTemplateRef> FunctionTemplateRef::parent()
46054604
return nullptr;
46064605
}
46074606
}
4607+
#endif
46084608

46094609
ScriptParserRef::InitializeScriptResult::InitializeScriptResult()
46104610
: script()

src/api/EscargotPublic.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ class ESCARGOT_EXPORT ExecutionStateRef {
636636
GCManagedVector<FunctionObjectRef*> resolveCallstack(); // resolve list of callee
637637
GlobalObjectRef* resolveCallerLexicalGlobalObject(); // resolve caller's lexical global object
638638

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

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

2042-
class ESCARGOT_EXPORT SerializerRef {
2043-
public:
2044-
// returns the serialization was successful
2045-
static bool serializeInto(ValueRef* value, std::ostringstream& output);
2046-
static ValueRef* deserializeFrom(ContextRef* context, std::istringstream& input);
2047-
};
2048-
2045+
// only enabled when `ENABLE_EXTENDED_API` macro is set (default: disabled)
20492046
class ESCARGOT_EXPORT ObjectTemplateRef : public TemplateRef {
20502047
public:
20512048
static ObjectTemplateRef* create();
@@ -2062,6 +2059,7 @@ class ESCARGOT_EXPORT ObjectTemplateRef : public TemplateRef {
20622059
};
20632060

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

2085+
class ESCARGOT_EXPORT SerializerRef {
2086+
public:
2087+
// returns the serialization was successful
2088+
static bool serializeInto(ValueRef* value, std::ostringstream& output);
2089+
static ValueRef* deserializeFrom(ContextRef* context, std::istringstream& input);
2090+
};
2091+
20872092
class ESCARGOT_EXPORT ScriptParserRef {
20882093
public:
20892094
struct ESCARGOT_EXPORT InitializeScriptResult {

src/builtins/BuiltinError.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,16 @@ static Value builtinErrorConstructor(ExecutionState& state, Value thisValue, siz
6363
Value options = argc > 1 ? argv[1] : Value();
6464
installErrorCause(state, obj, options);
6565

66+
#if defined(ENABLE_EXTENDED_API)
6667
if (UNLIKELY(state.context()->vmInstance()->isErrorCreationCallbackRegistered())) {
6768
state.context()->vmInstance()->triggerErrorCreationCallback(state, obj);
6869
}
70+
#endif
6971

7072
return obj;
7173
}
7274

75+
#if defined(ENABLE_EXTENDED_API)
7376
#define DEFINE_ERROR_CTOR(errorName, lowerCaseErrorName) \
7477
static Value builtin##errorName##ErrorConstructor(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget) \
7578
{ \
@@ -92,6 +95,27 @@ static Value builtinErrorConstructor(ExecutionState& state, Value thisValue, siz
9295
} \
9396
return obj; \
9497
}
98+
#else
99+
#define DEFINE_ERROR_CTOR(errorName, lowerCaseErrorName) \
100+
static Value builtin##errorName##ErrorConstructor(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget) \
101+
{ \
102+
if (!newTarget.hasValue()) { \
103+
newTarget = state.resolveCallee(); \
104+
} \
105+
Object* proto = Object::getPrototypeFromConstructor(state, newTarget.value(), [](ExecutionState& state, Context* constructorRealm) -> Object* { \
106+
return constructorRealm->globalObject()->lowerCaseErrorName##ErrorPrototype(); \
107+
}); \
108+
ErrorObject* obj = new errorName##ErrorObject(state, proto, String::emptyString); \
109+
Value message = argv[0]; \
110+
if (!message.isUndefined()) { \
111+
obj->defineOwnPropertyThrowsException(state, state.context()->staticStrings().message, \
112+
ObjectPropertyDescriptor(message.toString(state), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectStructurePropertyDescriptor::ConfigurablePresent))); \
113+
} \
114+
Value options = argc > 1 ? argv[1] : Value(); \
115+
installErrorCause(state, obj, options); \
116+
return obj; \
117+
}
118+
#endif
95119

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

158+
#if defined(ENABLE_EXTENDED_API)
134159
if (UNLIKELY(state.context()->vmInstance()->isErrorCreationCallbackRegistered())) {
135160
state.context()->vmInstance()->triggerErrorCreationCallback(state, O);
136161
}
162+
#endif
137163

138164
// Return O.
139165
return O;

src/interpreter/ByteCodeInterpreter.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ ALWAYS_INLINE size_t jumpTo(uint8_t* codeBuffer, const size_t jumpPosition)
7979
return (size_t)&codeBuffer[jumpPosition];
8080
}
8181

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

32953295
if (LIKELY(!code->m_isCatchResumeProcess && !code->m_isFinallyResumeProcess)) {
32963296
try {
3297-
#if defined(ESCARGOT_ENABLE_TEST)
3297+
#if defined(ENABLE_EXTENDED_API)
32983298
ExecutionStateVariableChanger<void (*)(ExecutionState&, bool)> changer(*state, [](ExecutionState& state, bool in) {
32993299
state.m_onTry = in;
33003300
});
@@ -3350,7 +3350,7 @@ NEVER_INLINE Value InterpreterSlowPath::tryOperation(ExecutionState*& state, siz
33503350
stackTraceDataVector.clear();
33513351
registerFile[code->m_catchedValueRegisterIndex] = val;
33523352
try {
3353-
#if defined(ESCARGOT_ENABLE_TEST)
3353+
#if defined(ENABLE_EXTENDED_API)
33543354
ExecutionStateVariableChanger<void (*)(ExecutionState&, bool)> changer(*state, [](ExecutionState& state, bool in) {
33553355
state.m_onCatch = in;
33563356
});
@@ -3367,7 +3367,7 @@ NEVER_INLINE Value InterpreterSlowPath::tryOperation(ExecutionState*& state, siz
33673367
}
33683368
} else if (code->m_isCatchResumeProcess) {
33693369
try {
3370-
#if defined(ESCARGOT_ENABLE_TEST)
3370+
#if defined(ENABLE_EXTENDED_API)
33713371
ExecutionStateVariableChanger<void (*)(ExecutionState&, bool)> changer(*state, [](ExecutionState& state, bool in) {
33723372
state.m_onCatch = in;
33733373
});
@@ -3388,7 +3388,7 @@ NEVER_INLINE Value InterpreterSlowPath::tryOperation(ExecutionState*& state, siz
33883388
}
33893389

33903390
if (code->m_isFinallyResumeProcess) {
3391-
#if defined(ESCARGOT_ENABLE_TEST)
3391+
#if defined(ENABLE_EXTENDED_API)
33923392
ExecutionStateVariableChanger<void (*)(ExecutionState&, bool)> changer(*state, [](ExecutionState& state, bool in) {
33933393
state.m_onFinally = in;
33943394
});
@@ -3411,7 +3411,7 @@ NEVER_INLINE Value InterpreterSlowPath::tryOperation(ExecutionState*& state, siz
34113411
newState = new ExtendedExecutionState(state, state->lexicalEnvironment(), state->inStrictMode());
34123412
newState->rareData()->setControlFlowRecordVector(state->rareData()->controlFlowRecordVector());
34133413
}
3414-
#if defined(ESCARGOT_ENABLE_TEST)
3414+
#if defined(ENABLE_EXTENDED_API)
34153415
ExecutionStateVariableChanger<void (*)(ExecutionState&, bool)> changer(*state, [](ExecutionState& state, bool in) {
34163416
state.m_onFinally = in;
34173417
});

src/runtime/Context.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,12 @@ void Context::throwException(ExecutionState& state, const Value& exception)
122122
{
123123
if (LIKELY(vmInstance()->currentSandBox() != nullptr)) {
124124
ASSERT(!!m_instance);
125+
#if defined(ENABLE_EXTENDED_API)
125126
if (UNLIKELY(m_instance->isErrorThrowCallbackRegistered() && exception.isObject() && exception.asObject()->isErrorObject())) {
126127
// trigger ErrorThrowCallback when an ErrorObject is thrown
127128
m_instance->triggerErrorThrowCallback(state, exception.asObject()->asErrorObject());
128129
}
130+
#endif
129131
vmInstance()->currentSandBox()->throwException(state, exception);
130132
} else {
131133
ESCARGOT_LOG_ERROR("there is no sandbox but exception occurred");

src/runtime/ErrorObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static Value builtinErrorObjectStackInfoSet(ExecutionState& state, Value thisVal
154154
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, "set Error.prototype.stack called on incompatible receiver");
155155
}
156156

157-
// Do noting
157+
// Do nothing
158158
return Value();
159159
}
160160

src/runtime/ExecutionState.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ ExecutionState::ExecutionState()
3737
, m_inStrictMode(false)
3838
, m_isNativeFunctionObjectExecutionContext(false)
3939
, m_inExecutionStopState(false)
40-
#if defined(ESCARGOT_ENABLE_TEST)
40+
#if defined(ENABLE_EXTENDED_API)
4141
, m_onTry(false)
4242
, m_onCatch(false)
4343
, m_onFinally(false)

src/runtime/ExecutionState.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class ExecutionState : public gc {
9191
, m_inStrictMode(inStrictMode)
9292
, m_isNativeFunctionObjectExecutionContext(false)
9393
, m_inExecutionStopState(false)
94-
#if defined(ESCARGOT_ENABLE_TEST)
94+
#if defined(ENABLE_EXTENDED_API)
9595
, m_onTry(false)
9696
, m_onCatch(false)
9797
, m_onFinally(false)
@@ -113,7 +113,7 @@ class ExecutionState : public gc {
113113
, m_inStrictMode(false)
114114
, m_isNativeFunctionObjectExecutionContext(false)
115115
, m_inExecutionStopState(false)
116-
#if defined(ESCARGOT_ENABLE_TEST)
116+
#if defined(ENABLE_EXTENDED_API)
117117
, m_onTry(false)
118118
, m_onCatch(false)
119119
, m_onFinally(false)
@@ -135,7 +135,7 @@ class ExecutionState : public gc {
135135
, m_inStrictMode(inStrictMode)
136136
, m_isNativeFunctionObjectExecutionContext(false)
137137
, m_inExecutionStopState(false)
138-
#if defined(ESCARGOT_ENABLE_TEST)
138+
#if defined(ENABLE_EXTENDED_API)
139139
, m_onTry(false)
140140
, m_onCatch(false)
141141
, m_onFinally(false)
@@ -157,7 +157,7 @@ class ExecutionState : public gc {
157157
, m_inStrictMode(inStrictMode)
158158
, m_isNativeFunctionObjectExecutionContext(true)
159159
, m_inExecutionStopState(false)
160-
#if defined(ESCARGOT_ENABLE_TEST)
160+
#if defined(ENABLE_EXTENDED_API)
161161
, m_onTry(false)
162162
, m_onCatch(false)
163163
, m_onFinally(false)
@@ -230,7 +230,7 @@ class ExecutionState : public gc {
230230
return m_inExecutionStopState;
231231
}
232232

233-
#if defined(ESCARGOT_ENABLE_TEST)
233+
#if defined(ENABLE_EXTENDED_API)
234234
bool onTry() const
235235
{
236236
return m_onTry;
@@ -332,7 +332,7 @@ class ExecutionState : public gc {
332332
bool m_inStrictMode : 1;
333333
bool m_isNativeFunctionObjectExecutionContext : 1;
334334
bool m_inExecutionStopState : 1;
335-
#if defined(ESCARGOT_ENABLE_TEST)
335+
#if defined(ENABLE_EXTENDED_API)
336336
bool m_onTry : 1;
337337
bool m_onCatch : 1;
338338
bool m_onFinally : 1;

0 commit comments

Comments
 (0)