forked from Samsung/escargot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJob.cpp
More file actions
141 lines (124 loc) · 5.65 KB
/
Job.cpp
File metadata and controls
141 lines (124 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (c) 2017-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 "Job.h"
#include "Context.h"
#include "VMInstance.h"
#include "SandBox.h"
#include "runtime/FinalizationRegistryObject.h"
namespace Escargot {
SandBox::SandBoxResult PromiseReactionJob::run()
{
Context* context = relatedContext();
ExecutionState state(context);
if (UNLIKELY(context->vmInstance()->isPromiseHookRegistered())) {
Object* promiseTarget = m_reaction.m_capability.m_promise;
PromiseObject* promise = (promiseTarget && promiseTarget->isPromiseObject()) ? promiseTarget->asPromiseObject() : nullptr;
context->vmInstance()->triggerPromiseHook(state, VMInstance::PromiseHookType::Before, promise, Value());
}
#ifdef ESCARGOT_DEBUGGER
Debugger* debugger = state.context()->debugger();
ExecutionState* activeSavedStackTraceExecutionState = ESCARGOT_DEBUGGER_NO_STACK_TRACE_RESTORE;
Debugger::SavedStackTraceDataVector* activeSavedStackTrace = nullptr;
if (debugger != nullptr && m_reaction.m_capability.m_savedStackTrace != nullptr) {
activeSavedStackTraceExecutionState = debugger->activeSavedStackTraceExecutionState();
activeSavedStackTrace = debugger->activeSavedStackTrace();
debugger->setActiveSavedStackTrace(&state, m_reaction.m_capability.m_savedStackTrace);
}
#endif /* ESCARGOT_DEBUGGER */
// https://www.ecma-international.org/ecma-262/10.0/#sec-promisereactionjob
SandBox sandbox(context);
SandBox::SandBoxResult result = sandbox.run([&]() -> Value {
/* 25.4.2.1.4 Handler is "Identity" case */
if (m_reaction.m_handler == (Object*)1) {
Value value[] = { m_argument };
return Object::call(state, m_reaction.m_capability.m_resolveFunction, Value(), 1, value);
}
/* 25.4.2.1.5 Handler is "Thrower" case */
if (m_reaction.m_handler == (Object*)2) {
Value value[] = { m_argument };
return Object::call(state, m_reaction.m_capability.m_rejectFunction, Value(), 1, value);
}
SandBox sb(state.context());
auto res = sb.run([&]() -> Value {
Value arguments[] = { m_argument };
Value res = Object::call(state, m_reaction.m_handler, Value(), 1, arguments);
// m_reaction.m_capability can be null when there was no result capability when promise.then()
if (m_reaction.m_capability.m_promise == nullptr) {
return Value();
}
Value value[] = { res };
return Object::call(state, m_reaction.m_capability.m_resolveFunction, Value(), 1, value);
});
if (!res.error.isEmpty()) {
if (m_reaction.m_capability.m_rejectFunction) {
Value reason[] = { res.error };
return Object::call(state, m_reaction.m_capability.m_rejectFunction, Value(), 1, reason);
} else {
state.throwException(res.error);
}
}
return res.result;
});
#ifdef ESCARGOT_DEBUGGER
if (activeSavedStackTraceExecutionState != ESCARGOT_DEBUGGER_NO_STACK_TRACE_RESTORE) {
debugger->setActiveSavedStackTrace(activeSavedStackTraceExecutionState, activeSavedStackTrace);
}
#endif /* ESCARGOT_DEBUGGER */
if (UNLIKELY(context->vmInstance()->isPromiseHookRegistered())) {
Object* promiseTarget = m_reaction.m_capability.m_promise;
PromiseObject* promise = (promiseTarget && promiseTarget->isPromiseObject()) ? promiseTarget->asPromiseObject() : nullptr;
context->vmInstance()->triggerPromiseHook(state, VMInstance::PromiseHookType::After, promise, Value());
}
return result;
}
SandBox::SandBoxResult PromiseResolveThenableJob::run()
{
// https://www.ecma-international.org/ecma-262/10.0/#sec-promiseresolvethenablejob
SandBox sandbox(relatedContext());
ExecutionState state(relatedContext());
return sandbox.run([&]() -> Value {
auto strings = &state.context()->staticStrings();
PromiseReaction::Capability capability = m_promise->createResolvingFunctions(state);
SandBox sb(state.context());
auto res = sb.run([&]() -> Value {
Value arguments[] = { capability.m_resolveFunction, capability.m_rejectFunction };
return Object::call(state, m_then, m_thenable, 2, arguments);
});
if (!res.error.isEmpty()) {
Value reason[] = { res.error };
return Object::call(state, capability.m_rejectFunction, Value(), 1, reason);
}
return res.result;
});
}
SandBox::SandBoxResult CleanupSomeJob::run()
{
auto oldCallback = m_object->m_cleanupCallback;
m_object->m_cleanupCallback = m_callback;
clearStack<1024>();
GC_gcollect_and_unmap();
GC_gcollect_and_unmap();
GC_gcollect_and_unmap();
m_object->m_cleanupCallback = oldCallback;
SandBox::SandBoxResult result;
result.result = Value();
return result;
}
} // namespace Escargot