aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/CommonJSModuleRecord.cpp
blob: 31c24bb669ad1022d91d20c9bd55f47b289ba084 (plain) (blame)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/**
 * How this works
 *
 * CommonJS modules are transpiled by Bun's transpiler to the following:
 *
 * (function (exports, require, module) { ... code })(exports, require, module)
 *
 * Then, at runtime, we create a JSCommonJSModule object.
 *
 * On this special object, we override the setter for the "exports" property in
 * a non-observable way (`static bool put ...`)
 *
 * When the setter is called, we set the internal "exports" property to the
 * value passed in and we also update the requireMap with the new value.
 *
 * After the CommonJS module is executed, we:
 * - Store the exports value in the requireMap (again)
 * - Loop through the keys of the exports object and re-export as ES Module
 *   named exports
 *
 * If an exception occurs, we remove the entry from the requireMap.
 *
 * We tried using a CustomGetterSetter instead of overriding `put`, but it led
 * to returning the getter itself
 *
 * How cyclical dependencies are handled
 *
 * Before executing the CommonJS module, we set the exports object in the
 * requireMap to an empty object. When the CommonJS module is required again, we
 * return the exports object from the requireMap. The values should be in sync
 * while the module is being executed, unless module.exports is re-assigned to a
 * different value. In that case, it will have a stale value.
 *
 */

#include "root.h"
#include "headers-handwritten.h"
#include "ZigGlobalObject.h"
#include "JavaScriptCore/JSSourceCode.h"
#include "JavaScriptCore/JSString.h"
#include "JavaScriptCore/JSValueInternal.h"
#include "JavaScriptCore/JSVirtualMachineInternal.h"
#include "JavaScriptCore/ObjectConstructor.h"
#include "JavaScriptCore/OptionsList.h"
#include "JavaScriptCore/ParserError.h"
#include "JavaScriptCore/ScriptExecutable.h"
#include "JavaScriptCore/SourceOrigin.h"
#include "JavaScriptCore/StackFrame.h"
#include "JavaScriptCore/StackVisitor.h"
#include "BunClientData.h"
#include "JavaScriptCore/Identifier.h"
#include "ImportMetaObject.h"

#include "JavaScriptCore/TypedArrayInlines.h"
#include "JavaScriptCore/PropertyNameArray.h"
#include "JavaScriptCore/JSWeakMap.h"
#include "JavaScriptCore/JSWeakMapInlines.h"
#include "JavaScriptCore/JSWithScope.h"

#include <JavaScriptCore/DFGAbstractHeap.h>
#include <JavaScriptCore/Completion.h>
#include <JavaScriptCore/JSMap.h>

#include <JavaScriptCore/JSMapInlines.h>
#include <JavaScriptCore/GetterSetter.h>
#include "ZigSourceProvider.h"

namespace Bun {
using namespace JSC;

static Structure* internalCreateCommonJSModuleStructure(
    Zig::GlobalObject* globalObject);

class JSCommonJSModule final : public JSC::JSNonFinalObject {
public:
    using Base = JSC::JSNonFinalObject;
    static constexpr unsigned StructureFlags = Base::StructureFlags | JSC::OverridesPut;

    mutable JSC::WriteBarrier<JSC::Unknown> m_exportsObject;
    mutable JSC::WriteBarrier<JSC::JSString> m_id;
    mutable JSC::WriteBarrier<JSC::EvalExecutable> m_executable;

    void finishCreation(JSC::VM& vm, JSC::JSValue exportsObject, JSC::JSString* id, JSC::JSString* filename, JSC::JSValue requireFunction, JSC::EvalExecutable* executable)
    {
        Base::finishCreation(vm);
        ASSERT(inherits(vm, info()));
        m_exportsObject.set(vm, this, exportsObject);
        m_id.set(vm, this, id);
        m_executable.set(vm, this, executable);

        this->putDirectOffset(
            vm,
            0,
            exportsObject);

        this->putDirectOffset(
            vm,
            1,
            id);

        this->putDirectOffset(
            vm,
            2,
            id);
        this->putDirectOffset(
            vm,
            3,
            filename);
        this->putDirectOffset(
            vm,
            4,
            requireFunction);
    }

    static JSCommonJSModule* create(
        JSC::VM& vm,
        JSC::Structure* structure,
        JSC::JSValue exportsObject,
        JSC::JSString* id,
        JSC::JSString* filename,
        JSC::JSValue requireFunction,
        JSC::EvalExecutable* executable)
    {
        JSCommonJSModule* cell = new (NotNull, JSC::allocateCell<JSCommonJSModule>(vm)) JSCommonJSModule(vm, structure);
        cell->finishCreation(vm, exportsObject, id, filename, requireFunction, executable);
        return cell;
    }

    JSValue exportsObject()
    {
        return m_exportsObject.get();
    }

    JSValue id()
    {
        return m_id.get();
    }

    DECLARE_VISIT_CHILDREN;

    static bool put(
        JSC::JSCell* cell,
        JSC::JSGlobalObject* globalObject,
        JSC::PropertyName propertyName,
        JSC::JSValue value,
        JSC::PutPropertySlot& slot)
    {
        JSCommonJSModule* thisObject = jsCast<JSCommonJSModule*>(cell);
        ASSERT_GC_OBJECT_INHERITS(thisObject, info());
        auto& vm = globalObject->vm();
        auto throwScope = DECLARE_THROW_SCOPE(vm);

        auto* clientData = WebCore::clientData(vm);
        bool result = Base::put(thisObject, globalObject, propertyName, value, slot);
        if (result) {
            // Whenever you call module.exports = ... in a module, we need to:
            //
            // - Update the internal exports object
            // - Update the require map
            //
            if (propertyName == clientData->builtinNames().exportsPublicName()) {
                thisObject->m_exportsObject.set(vm, thisObject, value);
                Zig::GlobalObject* zigGlobalObject = jsCast<Zig::GlobalObject*>(globalObject);
                zigGlobalObject->requireMap()->set(globalObject, thisObject->id(), value);
                RETURN_IF_EXCEPTION(throwScope, false);
            }
        }

        RELEASE_AND_RETURN(throwScope, result);
    }

    static JSC::Structure* createStructure(
        JSC::JSGlobalObject* globalObject)
    {
        return internalCreateCommonJSModuleStructure(reinterpret_cast<Zig::GlobalObject*>(globalObject));
    }

    DECLARE_INFO;
    template<typename, SubspaceAccess mode> static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
    {
        if constexpr (mode == JSC::SubspaceAccess::Concurrently)
            return nullptr;
        return WebCore::subspaceForImpl<JSCommonJSModule, WebCore::UseCustomHeapCellType::No>(
            vm,
            [](auto& spaces) { return spaces.m_clientSubspaceForCommonJSModuleRecord.get(); },
            [](auto& spaces, auto&& space) { spaces.m_clientSubspaceForCommonJSModuleRecord = std::forward<decltype(space)>(space); },
            [](auto& spaces) { return spaces.m_subspaceForCommonJSModuleRecord.get(); },
            [](auto& spaces, auto&& space) { spaces.m_subspaceForCommonJSModuleRecord = std::forward<decltype(space)>(space); });
    }

    JSCommonJSModule(JSC::VM& vm, JSC::Structure* structure)
        : Base(vm, structure)
    {
    }
};

Structure* createCommonJSModuleStructure(
    Zig::GlobalObject* globalObject)
{
    return JSCommonJSModule::createStructure(globalObject);
}

static Structure* internalCreateCommonJSModuleStructure(
    Zig::GlobalObject* globalObject)
{
    auto& vm = globalObject->vm();
    JSC::Structure* structure = JSC::Structure::create(
        vm,
        globalObject,
        globalObject->objectPrototype(),
        JSC::TypeInfo(JSC::ObjectType, JSCommonJSModule::StructureFlags),
        JSCommonJSModule::info(),
        JSC::NonArray,
        4);

    JSC::PropertyOffset offset;
    auto clientData = WebCore::clientData(vm);

    structure = structure->addPropertyTransition(
        vm,
        structure,
        JSC::Identifier::fromString(vm, "exports"_s),
        0,
        offset);

    structure = structure->addPropertyTransition(
        vm,
        structure,
        JSC::Identifier::fromString(vm, "id"_s),
        0,
        offset);

    structure = structure->addPropertyTransition(
        vm,
        structure,
        JSC::Identifier::fromString(vm, "filename"_s),
        0,
        offset);

    structure = structure->addPropertyTransition(
        vm,
        structure,
        JSC::Identifier::fromString(vm, "require"_s),
        JSC::PropertyAttribute::Builtin | JSC::PropertyAttribute::Function | 0,
        offset);

    return structure;
}

template<typename Visitor>
void JSCommonJSModule::visitChildrenImpl(JSCell* cell, Visitor& visitor)
{
    JSCommonJSModule* thisObject = jsCast<JSCommonJSModule*>(cell);
    ASSERT_GC_OBJECT_INHERITS(thisObject, info());
    Base::visitChildren(thisObject, visitor);
    visitor.append(thisObject->m_exportsObject);
    visitor.append(thisObject->m_id);
    visitor.append(thisObject->m_executable);
}

DEFINE_VISIT_CHILDREN(JSCommonJSModule);
const JSC::ClassInfo JSCommonJSModule::s_info = { "Module"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCommonJSModule) };

JSCommonJSModule* createCommonJSModuleObject(
    Zig::GlobalObject* globalObject,
    const ResolvedSource& source,
    const WTF::String& sourceURL,
    JSC::JSValue exportsObjectValue,
    JSC::JSValue requireFunctionValue,
    JSC::EvalExecutable* executable,
    JSC::JSString* filename)
{
    auto& vm = globalObject->vm();
    auto scope = DECLARE_THROW_SCOPE(vm);
    auto* jsSourceURL = JSC::jsString(vm, sourceURL);

    JSCommonJSModule* moduleObject = JSCommonJSModule::create(
        vm,
        globalObject->CommonJSModuleObjectStructure(),
        exportsObjectValue,
        jsSourceURL, filename, requireFunctionValue, executable);

    return moduleObject;
}

static bool canPerformFastEnumeration(Structure* s)
{
    if (s->typeInfo().overridesGetOwnPropertySlot())
        return false;
    if (s->typeInfo().overridesAnyFormOfGetOwnPropertyNames())
        return false;
    if (hasIndexedProperties(s->indexingType()))
        return false;
    if (s->hasAnyKindOfGetterSetterProperties())
        return false;
    if (s->isUncacheableDictionary())
        return false;
    if (s->hasUnderscoreProtoPropertyExcludingOriginalProto())
        return false;
    return true;
}

JSC::SourceCode createCommonJSModule(
    Zig::GlobalObject* globalObject,
    ResolvedSource source)
{
    auto sourceURL = Zig::toStringCopy(source.source_url);
    auto sourceProvider = Zig::SourceProvider::create(globalObject, source, JSC::SourceProviderSourceType::Program);

    return JSC::SourceCode(
        JSC::SyntheticSourceProvider::create(
            [source, sourceProvider = WTFMove(sourceProvider), sourceURL](JSC::JSGlobalObject* lexicalGlobalObject,
                JSC::Identifier moduleKey,
                Vector<JSC::Identifier, 4>& exportNames,
                JSC::MarkedArgumentBuffer& exportValues) -> void {
                auto* globalObject = jsCast<Zig::GlobalObject*>(lexicalGlobalObject);
                auto& vm = globalObject->vm();

                auto throwScope = DECLARE_THROW_SCOPE(vm);
                auto* requireMapKey = jsString(vm, sourceURL);

                JSC::JSObject* exportsObject = source.commonJSExportsLen < 64
                    ? JSC::constructEmptyObject(globalObject, globalObject->objectPrototype(), source.commonJSExportsLen)
                    : JSC::constructEmptyObject(globalObject, globalObject->objectPrototype());
                auto index = sourceURL.reverseFind('/', sourceURL.length());
                JSString* dirname = jsEmptyString(vm);
                JSString* filename = requireMapKey;
                if (index != WTF::notFound) {
                    dirname = JSC::jsSubstring(globalObject, requireMapKey, 0, index);
                }

                globalObject->requireMap()->set(globalObject, requireMapKey, exportsObject);
                JSC::SourceCode inputSource(
                    WTFMove(sourceProvider));

                JSC::Structure* scopeExtensionObjectStructure = globalObject->commonJSFunctionArgumentsStructure();
                JSC::JSObject* scopeExtensionObject = JSC::constructEmptyObject(
                    vm,
                    scopeExtensionObjectStructure);

                auto* requireFunction = Zig::ImportMetaObject::createRequireFunction(vm, globalObject, sourceURL);
                auto* executable = JSC::DirectEvalExecutable::create(
                    globalObject, inputSource, DerivedContextType::None, NeedsClassFieldInitializer::No, PrivateBrandRequirement::None,
                    false, false, EvalContextType::None, nullptr, nullptr, ECMAMode::sloppy());

                if (UNLIKELY(!executable && !throwScope.exception())) {
                    // I'm not sure if this case happens, but it's better to be safe than sorry.
                    throwSyntaxError(globalObject, throwScope, "Failed to compile CommonJS module."_s);
                }

                if (UNLIKELY(throwScope.exception())) {
                    globalObject->requireMap()->remove(globalObject, requireMapKey);
                    throwScope.release();
                    return;
                }

                auto* moduleObject = createCommonJSModuleObject(globalObject,
                    source,
                    sourceURL,
                    exportsObject,
                    requireFunction, executable, filename);

                scopeExtensionObject->putDirectOffset(
                    vm,
                    0,
                    moduleObject);

                scopeExtensionObject->putDirectOffset(
                    vm,
                    1,
                    exportsObject);

                scopeExtensionObject->putDirectOffset(
                    vm,
                    2,
                    dirname);

                scopeExtensionObject->putDirectOffset(
                    vm,
                    3,
                    filename);

                scopeExtensionObject->putDirectOffset(
                    vm,
                    4,
                    requireFunction);

                if (UNLIKELY(throwScope.exception())) {
                    globalObject->requireMap()->remove(globalObject, requireMapKey);
                    throwScope.release();
                    return;
                }

                auto catchScope = DECLARE_CATCH_SCOPE(vm);

                // Where the magic happens.
                //
                // A `with` scope is created containing { module, exports, require }.
                // We eval() the CommonJS module code
                // with that scope.
                //
                // Doing it that way saves us a roundtrip through C++ <> JS.
                //
                //      Sidenote: another implementation could use
                //      FunctionExecutable. It looks like there are lots of arguments
                //      to pass to that and it isn't used directly much, so that
                //      seems harder to do correctly.
                {
                    // We must use a global scope extension or else the JSWithScope will be collected unexpectedly.
                    // https://github.com/oven-sh/bun/issues/3161
                    globalObject->clearGlobalScopeExtension();

                    JSWithScope* withScope = JSWithScope::create(vm, globalObject, globalObject->globalScope(), scopeExtensionObject);
                    globalObject->setGlobalScopeExtension(withScope);
                    vm.interpreter.executeEval(executable, globalObject, globalObject->globalScope());
                    globalObject->clearGlobalScopeExtension();

                    if (UNLIKELY(catchScope.exception())) {
                        auto returnedException = catchScope.exception();
                        catchScope.clearException();
                        JSC::throwException(globalObject, throwScope, returnedException);
                    }
                }

                if (throwScope.exception()) {
                    globalObject->requireMap()->remove(globalObject, requireMapKey);
                    throwScope.release();
                    return;
                }

                JSValue result = moduleObject->exportsObject();

                // The developer can do something like:
                //
                //   Object.defineProperty(module, 'exports', {get: getter})
                //
                // In which case, the exports object is now a GetterSetter object.
                //
                // We can't return a GetterSetter object to ESM code, so we need to call it.
                if (!result.isEmpty() && (result.isGetterSetter() || result.isCustomGetterSetter())) {
                    auto* clientData = WebCore::clientData(vm);

                    // TODO: is there a faster way to call these getters? We shouldn't need to do a full property lookup.
                    //
                    // we use getIfPropertyExists just incase a pathological devleoper did:
                    //
                    //   - Object.defineProperty(module, 'exports', {get: getter})
                    //   - delete module.exports
                    //
                    if (result.isGetterSetter()) {
                        JSC::GetterSetter* getter = jsCast<JSC::GetterSetter*>(result);
                        result = getter->callGetter(globalObject, moduleObject);
                    } else {
                        result = moduleObject->getIfPropertyExists(globalObject, clientData->builtinNames().exportsPublicName());
                    }

                    if (UNLIKELY(throwScope.exception())) {
                        // Unlike getters on properties of the exports object
                        // When the exports object itself is a getter and it throws
                        // There's not a lot we can do
                        // so we surface that error
                        globalObject->requireMap()->remove(globalObject, requireMapKey);
                        throwScope.release();
                        return;
                    }
                }

                globalObject->requireMap()->set(globalObject, requireMapKey, result);

                exportNames.append(vm.propertyNames->defaultKeyword);
                exportValues.append(result);

                // This exists to tell ImportMetaObject.ts that this is a CommonJS module.
                exportNames.append(Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s)));
                exportValues.append(jsNumber(0));

                moduleObject->m_executable.clear();

                if (result.isObject()) {
                    auto* exports = asObject(result);

                    auto* structure = exports->structure();
                    uint32_t size = structure->inlineSize() + structure->outOfLineSize();
                    exportNames.reserveCapacity(size + 2);
                    exportValues.ensureCapacity(size + 2);

                    if (canPerformFastEnumeration(structure)) {
                        exports->structure()->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool {
                            auto key = entry.key();
                            if (key->isSymbol() || key == vm.propertyNames->defaultKeyword || entry.attributes() & PropertyAttribute::DontEnum)
                                return true;

                            exportNames.append(Identifier::fromUid(vm, key));

                            JSValue value = exports->getDirect(entry.offset());

                            exportValues.append(value);
                            return true;
                        });
                    } else {
                        JSC::PropertyNameArray properties(vm, JSC::PropertyNameMode::Strings, JSC::PrivateSymbolMode::Exclude);
                        exports->methodTable()->getOwnPropertyNames(exports, globalObject, properties, DontEnumPropertiesMode::Exclude);
                        if (throwScope.exception()) {
                            throwScope.release();
                            return;
                        }

                        for (auto property : properties) {
                            if (UNLIKELY(property.isEmpty() || property.isNull()))
                                continue;

                            // ignore constructor
                            if (property == vm.propertyNames->constructor)
                                continue;

                            if (property.isSymbol() || property.isPrivateName() || property == vm.propertyNames->defaultKeyword)
                                continue;

                            JSC::PropertySlot slot(exports, PropertySlot::InternalMethodType::Get);
                            if (!exports->getPropertySlot(globalObject, property, slot))
                                continue;

                            exportNames.append(property);

                            JSValue getterResult = slot.getValue(globalObject, property);

                            // If it throws, we keep them in the exports list, but mark it as undefined
                            // This is consistent with what Node.js does.
                            if (catchScope.exception()) {
                                catchScope.clearException();
                                getterResult = jsUndefined();
                            }

                            exportValues.append(getterResult);
                        }
                    }
                }
            },
            SourceOrigin(WTF::URL::fileURLWithFileSystemPath(sourceURL)),
            sourceURL));
}

}