forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSAPIGlobalObject.mm
More file actions
296 lines (244 loc) · 12.6 KB
/
JSAPIGlobalObject.mm
File metadata and controls
296 lines (244 loc) · 12.6 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* Copyright (C) 2019-2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "config.h"
#import "JSAPIGlobalObject.h"
#if JSC_OBJC_API_ENABLED
#import "APICast.h"
#import "CallFrameInlines.h"
#import "CatchScope.h"
#import "Completion.h"
#import "Error.h"
#import "Exception.h"
#import "GlobalObjectMethodTable.h"
#import "IdentifierInlines.h"
#import "JSContextInternal.h"
#import "JSInternalPromise.h"
#import "JSModuleLoader.h"
#import "JSNativeStdFunction.h"
#import "JSObjectInlines.h"
#import "JSPromise.h"
#import "JSScriptFetchParameters.h"
#import "JSScriptInternal.h"
#import "JSSourceCode.h"
#import "JSValueInternal.h"
#import "JSVirtualMachineInternal.h"
#import "JavaScriptCore.h"
#import "ObjectConstructor.h"
#import "SourceOrigin.h"
#import "StrongInlines.h"
#import <wtf/URL.h>
namespace JSC {
const GlobalObjectMethodTable* JSAPIGlobalObject::globalObjectMethodTable()
{
static constexpr GlobalObjectMethodTable table = {
&supportsRichSourceInfo,
&shouldInterruptScript,
&javaScriptRuntimeFlags,
nullptr, // queueMicrotaskToEventLoop
&shouldInterruptScriptBeforeTimeout,
&moduleLoaderImportModule, // moduleLoaderImportModule
&moduleLoaderResolve, // moduleLoaderResolve
&moduleLoaderFetch, // moduleLoaderFetch
&moduleLoaderCreateImportMetaProperties, // moduleLoaderCreateImportMetaProperties
&moduleLoaderEvaluate, // moduleLoaderEvaluate
nullptr, // promiseRejectionTracker
&reportUncaughtExceptionAtEventLoop,
¤tScriptExecutionOwner,
&scriptExecutionStatus,
&reportViolationForUnsafeEval,
nullptr, // defaultLanguage
nullptr, // compileStreaming
nullptr, // instantiateStreaming
&deriveShadowRealmGlobalObject,
};
return &table;
};
void JSAPIGlobalObject::reportUncaughtExceptionAtEventLoop(JSGlobalObject* globalObject, Exception* exception)
{
JSContext *context = [JSContext contextWithJSGlobalContextRef:toGlobalRef(globalObject)];
[context notifyException:toRef(globalObject->vm(), exception->value())];
}
static Expected<URL, String> computeValidImportSpecifier(const URL& base, const String& specifier)
{
URL absoluteURL(specifier);
if (absoluteURL.isValid())
return absoluteURL;
if (!specifier.startsWith('/') && !specifier.startsWith("./"_s) && !specifier.startsWith("../"_s))
return makeUnexpected(makeString("Module specifier: "_s, specifier, " does not start with \"/\", \"./\", or \"../\". Referenced from: "_s, base.string()));
if (specifier.startsWith('/')) {
absoluteURL = URL(URL("file://"_s), specifier);
if (absoluteURL.isValid())
return absoluteURL;
}
if (base == URL())
return makeUnexpected("Could not determine the base URL for loading."_s);
if (!base.isValid())
return makeUnexpected(makeString("Referrering script's url is not valid: "_s, base.string()));
absoluteURL = URL(base, specifier);
if (absoluteURL.isValid())
return absoluteURL;
return makeUnexpected(makeString("Could not form valid URL from identifier and base. Tried:"_s, absoluteURL.string()));
}
Identifier JSAPIGlobalObject::moduleLoaderResolve(JSGlobalObject* globalObject, JSModuleLoader*, JSValue key, JSValue referrer, JSValue)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT_UNUSED(globalObject, globalObject == globalObject);
ASSERT(key.isString() || key.isSymbol());
String name = key.toWTFString(globalObject);
RETURN_IF_EXCEPTION(scope, { });
URL base;
if (JSString* referrerString = jsDynamicCast<JSString*>(referrer)) {
String value = referrerString->value(globalObject);
RETURN_IF_EXCEPTION(scope, { });
// It can be invalid URL because dynamic-import will be resolved with caller's source origin (this becomes referrer), and it can be non valid URL.
// But this is handled well in computeValidImportSpecifier.
base = URL { { }, value };
}
auto result = computeValidImportSpecifier(base, name);
if (result)
return Identifier::fromString(vm, result.value().string());
throwVMError(globalObject, scope, createError(globalObject, result.error()));
return { };
}
JSInternalPromise* JSAPIGlobalObject::moduleLoaderImportModule(JSGlobalObject* globalObject, JSModuleLoader*, JSString* specifierValue, JSValue parameters, const SourceOrigin& sourceOrigin)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto reject = [&] (ThrowScope& scope) -> JSInternalPromise* {
auto* promise = JSInternalPromise::create(vm, globalObject->internalPromiseStructure());
return promise->rejectWithCaughtException(globalObject, scope);
};
auto import = [&] (const String& specifier, JSValue parameters) {
auto result = importModule(globalObject, Identifier::fromString(vm, specifier), jsString(vm, sourceOrigin.url().string()), parameters, jsUndefined());
RETURN_IF_EXCEPTION(scope, reject(scope));
return result;
};
auto specifier = specifierValue->value(globalObject);
RETURN_IF_EXCEPTION(scope, reject(scope));
auto attributes = JSC::retrieveImportAttributesFromDynamicImportOptions(globalObject, parameters, { vm.propertyNames->type.impl() });
RETURN_IF_EXCEPTION(scope, reject(scope));
auto type = JSC::retrieveTypeImportAttribute(globalObject, attributes);
RETURN_IF_EXCEPTION(scope, reject(scope));
parameters = jsUndefined();
if (type)
parameters = JSScriptFetchParameters::create(vm, ScriptFetchParameters::create(type.value()));
return import(specifier, parameters);
}
JSInternalPromise* JSAPIGlobalObject::moduleLoaderFetch(JSGlobalObject* globalObject, JSModuleLoader*, JSValue key, JSValue, JSValue)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(globalObject == globalObject);
JSContext *context = [JSContext contextWithJSGlobalContextRef:toGlobalRef(globalObject)];
JSInternalPromise* promise = JSInternalPromise::create(vm, globalObject->internalPromiseStructure());
Identifier moduleKey = key.toPropertyKey(globalObject);
RETURN_IF_EXCEPTION(scope, promise->rejectWithCaughtException(globalObject, scope));
if (UNLIKELY(![context moduleLoaderDelegate])) {
scope.release();
promise->reject(globalObject, createError(globalObject, "No module loader provided."_s));
return promise;
}
auto strongPromise = Strong<JSInternalPromise>(vm, promise);
auto* resolve = JSNativeStdFunction::create(vm, globalObject, 1, "resolve"_s, [=] (JSGlobalObject* globalObject, CallFrame* callFrame) {
// This captures the globalObject but that's ok because our structure keeps it alive anyway.
VM& vm = globalObject->vm();
JSContext *context = [JSContext contextWithJSGlobalContextRef:toGlobalRef(globalObject)];
id script = valueToObject(context, toRef(globalObject, callFrame->argument(0)));
auto rejectPromise = [&] (String message) {
strongPromise.get()->reject(globalObject, createTypeError(globalObject, message));
return encodedJSUndefined();
};
if (UNLIKELY(![script isKindOfClass:[JSScript class]]))
return rejectPromise("First argument of resolution callback is not a JSScript"_s);
JSScript* jsScript = static_cast<JSScript *>(script);
JSSourceCode* source = [jsScript jsSourceCode];
if (UNLIKELY([jsScript type] != kJSScriptTypeModule))
return rejectPromise("The JSScript that was provided did not have expected type of kJSScriptTypeModule."_s);
NSURL *sourceURL = [jsScript sourceURL];
String oldModuleKey { [sourceURL absoluteString] };
if (UNLIKELY(Identifier::fromString(vm, oldModuleKey) != moduleKey))
return rejectPromise(makeString("The same JSScript was provided for two different identifiers, previously: ", oldModuleKey, " and now: ", moduleKey.string()));
strongPromise.get()->resolve(globalObject, source);
return encodedJSUndefined();
});
auto* reject = JSNativeStdFunction::create(vm, globalObject, 1, "reject"_s, [=] (JSGlobalObject*, CallFrame* callFrame) {
strongPromise.get()->reject(globalObject, callFrame->argument(0));
return encodedJSUndefined();
});
[[context moduleLoaderDelegate] context:context fetchModuleForIdentifier:[::JSValue valueWithJSValueRef:toRef(globalObject, key) inContext:context] withResolveHandler:[::JSValue valueWithJSValueRef:toRef(globalObject, resolve) inContext:context] andRejectHandler:[::JSValue valueWithJSValueRef:toRef(globalObject, reject) inContext:context]];
if (context.exception) {
scope.release();
promise->reject(globalObject, toJS(globalObject, [context.exception JSValueRef]));
context.exception = nil;
}
return promise;
}
JSObject* JSAPIGlobalObject::moduleLoaderCreateImportMetaProperties(JSGlobalObject* globalObject, JSModuleLoader*, JSValue key, JSModuleRecord*, JSValue)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSObject* metaProperties = constructEmptyObject(vm, globalObject->nullPrototypeObjectStructure());
RETURN_IF_EXCEPTION(scope, nullptr);
metaProperties->putDirect(vm, Identifier::fromString(vm, "filename"_s), key);
RETURN_IF_EXCEPTION(scope, nullptr);
return metaProperties;
}
JSValue JSAPIGlobalObject::moduleLoaderEvaluate(JSGlobalObject* globalObject, JSModuleLoader* moduleLoader, JSValue key, JSValue moduleRecordValue, JSValue scriptFetcher, JSValue sentValue, JSValue resumeMode)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSContext *context = [JSContext contextWithJSGlobalContextRef:toGlobalRef(globalObject)];
id <JSModuleLoaderDelegate> moduleLoaderDelegate = [context moduleLoaderDelegate];
NSURL *url = nil;
if ([moduleLoaderDelegate respondsToSelector:@selector(willEvaluateModule:)] || [moduleLoaderDelegate respondsToSelector:@selector(didEvaluateModule:)]) {
String moduleKey = key.toWTFString(globalObject);
RETURN_IF_EXCEPTION(scope, { });
url = [NSURL URLWithString:static_cast<NSString *>(moduleKey)];
}
if ([moduleLoaderDelegate respondsToSelector:@selector(willEvaluateModule:)])
[moduleLoaderDelegate willEvaluateModule:url];
scope.release();
// FIXME: We should update the delegate callbacks for async modules. https://bugs.webkit.org/show_bug.cgi?id=222253
JSValue result = moduleLoader->evaluateNonVirtual(globalObject, key, moduleRecordValue, scriptFetcher, sentValue, resumeMode);
if ([moduleLoaderDelegate respondsToSelector:@selector(didEvaluateModule:)])
[moduleLoaderDelegate didEvaluateModule:url];
return result;
}
JSValue JSAPIGlobalObject::loadAndEvaluateJSScriptModule(const JSLockHolder&, JSScript *script)
{
ASSERT(script.type == kJSScriptTypeModule);
VM& vm = this->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
Identifier key = Identifier::fromString(vm, String { [[script sourceURL] absoluteString] });
JSInternalPromise* promise = importModule(this, key, jsUndefined(), jsUndefined(), jsUndefined());
RETURN_IF_EXCEPTION(scope, { });
auto* result = JSPromise::create(vm, this->promiseStructure());
result->resolve(this, promise);
RETURN_IF_EXCEPTION(scope, { });
return result;
}
}
#endif // JSC_OBJC_API_ENABLED