aboutsummaryrefslogtreecommitdiff
path: root/src/javascript/jsc/bindings/ZigGlobalObject.cpp
blob: e17e3aeba742c63f47242d74886816b263cdff06 (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

#include "ZigGlobalObject.h"
#include "helpers.h"

#include <JavaScriptCore/CallFrameInlines.h>
#include <JavaScriptCore/CatchScope.h>
#include <JavaScriptCore/Completion.h>
#include <JavaScriptCore/Error.h>
#include <JavaScriptCore/Exception.h>
#include <JavaScriptCore/JSContextInternal.h>
#include <JavaScriptCore/JSInternalPromise.h>
#include <JavaScriptCore/JSModuleLoader.h>
#include <JavaScriptCore/JSNativeStdFunction.h>
#include <JavaScriptCore/JSPromise.h>
#include <JavaScriptCore/JSSourceCode.h>
#include <JavaScriptCore/JSValueInternal.h>
#include <JavaScriptCore/JSVirtualMachineInternal.h>
#include <JavaScriptCore/ObjectConstructor.h>
#include <JavaScriptCore/SourceOrigin.h>
#include <JavaScriptCore/Identifier.h>
#include <wtf/URL.h>
#include <JavaScriptCore/ClassInfo.h>
#include <JavaScriptCore/JSString.h>
#include <JavaScriptCore/VM.h>
#include <JavaScriptCore/WasmFaultSignalHandler.h>
#include <JavaScriptCore/JSCast.h>
#include <JavaScriptCore/InitializeThreading.h>

#include <JavaScriptCore/JSLock.h>

using JSGlobalObject = JSC::JSGlobalObject;
using Exception = JSC::Exception;
using JSValue = JSC::JSValue;
using JSString = JSC::JSString;
using JSModuleLoader = JSC::JSModuleLoader;
using JSModuleRecord = JSC::JSModuleRecord;
using Identifier = JSC::Identifier;
using SourceOrigin = JSC::SourceOrigin;
namespace JSCastingHelpers = JSC::JSCastingHelpers;


JSC__JSGlobalObject* Zig__GlobalObject__create(JSC__VM* arg0) {
    // There are assertions that the apiLock is set while the JSGlobalObject is initialized.
    if (arg0 != nullptr) {
        JSC::VM& vm = reinterpret_cast<JSC__VM&>(arg0);
        vm.apiLock().lock();
        Zig::GlobalObject* globalObject = Zig::GlobalObject::create(vm, Zig::GlobalObject::createStructure(vm, JSC::jsNull()));
        vm.apiLock().unlock();
        return static_cast<JSC__JSGlobalObject*>(globalObject);
    }

    JSC::initialize();
    
    JSC::VM& vm = JSC::VM::create(JSC::LargeHeap, nullptr);
    vm.apiLock().lock();


      #if ENABLE(WEBASSEMBLY)
        JSC::Wasm::enableFastMemory();
    #endif

    Zig::GlobalObject* globalObject = Zig::GlobalObject::create(vm, Zig::GlobalObject::createStructure(vm, JSC::jsNull()));
    vm.apiLock().unlock();
    return static_cast<JSC__JSGlobalObject*>(globalObject);
}

namespace Zig {

const JSC::ClassInfo GlobalObject::s_info = { "GlobalObject", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(GlobalObject) };

const JSC::GlobalObjectMethodTable GlobalObject::s_globalObjectMethodTable = {
    &supportsRichSourceInfo,
    &shouldInterruptScript,
    &javaScriptRuntimeFlags,
    nullptr, // queueTaskToEventLoop
nullptr,    // &shouldInterruptScriptBeforeTimeout,
    &moduleLoaderImportModule, // moduleLoaderImportModule
    &moduleLoaderResolve, // moduleLoaderResolve
    &moduleLoaderFetch, // moduleLoaderFetch
    &moduleLoaderCreateImportMetaProperties, // moduleLoaderCreateImportMetaProperties
    &moduleLoaderEvaluate, // moduleLoaderEvaluate
    &promiseRejectionTracker, // promiseRejectionTracker
    &reportUncaughtExceptionAtEventLoop,
    &currentScriptExecutionOwner,
    &scriptExecutionStatus,
    nullptr, // defaultLanguage
    nullptr, // compileStreaming
    nullptr, // instantiateStreaming
};

void GlobalObject::reportUncaughtExceptionAtEventLoop(JSGlobalObject* globalObject, Exception* exception) {
    Zig__GlobalObject__reportUncaughtException(globalObject, exception);
}

void GlobalObject::promiseRejectionTracker(JSGlobalObject* obj, JSC::JSPromise* prom, JSC::JSPromiseRejectionOperation reject) {
    Zig__GlobalObject__promiseRejectionTracker(obj, prom, reject == JSC::JSPromiseRejectionOperation::Reject ? 0 : 1);
}

JSC::Identifier GlobalObject::moduleLoaderResolve(
    JSGlobalObject* globalObject,
    JSModuleLoader* loader,
    JSValue key,
    JSValue referrer,
    JSValue origin
) {
    auto res = Zig__GlobalObject__resolve(
        globalObject,
        loader,
        JSValue::encode(key),
        JSValue::encode(referrer),
        nullptr
    );

   Wrap<JSC::Identifier, bJSC__Identifier> wrapped = Wrap<JSC::Identifier, bJSC__Identifier>(res);
   return *wrapped.cpp;
}

JSC::JSInternalPromise* GlobalObject::moduleLoaderImportModule(JSGlobalObject* globalObject, JSModuleLoader* loader, JSString* specifierValue, JSValue referrer, const SourceOrigin& sourceOrigin) {
    return Zig__GlobalObject__import(
        globalObject,
        loader,
        specifierValue,
        JSC::JSValue::encode(referrer),
        &sourceOrigin
    );
}

JSC::JSInternalPromise* GlobalObject::moduleLoaderFetch(JSGlobalObject* globalObject, JSModuleLoader* loader, JSValue key, JSValue value1, JSValue value2) {
    return Zig__GlobalObject__fetch(
        globalObject,
        loader,
        JSValue::encode(key),
        JSValue::encode(value1),
        JSValue::encode(value2)
    );
}

JSC::JSObject* GlobalObject::moduleLoaderCreateImportMetaProperties(JSGlobalObject* globalObject, JSModuleLoader* loader, JSValue key, JSModuleRecord* record, JSValue val) {
    auto res = Zig__GlobalObject__createImportMetaProperties(
        globalObject,
        loader,
        JSValue::encode(key),
        record,
        JSValue::encode(val)
    );

    return JSValue::decode(res).getObject();
}

JSC::JSValue GlobalObject::moduleLoaderEvaluate(JSGlobalObject* globalObject, JSModuleLoader* moduleLoader, JSValue key, JSValue moduleRecordValue, JSValue scriptFetcher, JSValue sentValue, JSValue resumeMode) {
    auto res = Zig__GlobalObject__eval(
        globalObject,
        moduleLoader,
        JSValue::encode(key),
        JSValue::encode(moduleRecordValue),
        JSValue::encode(scriptFetcher),
        JSValue::encode(sentValue),
        JSValue::encode(resumeMode)
    );

    return JSValue::decode(res);
}

}