This repository was archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcontext.cpp
More file actions
179 lines (157 loc) · 6.2 KB
/
Copy pathcontext.cpp
File metadata and controls
179 lines (157 loc) · 6.2 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
/*
* Nexus.js - The next-gen JavaScript platform
* Copyright (C) 2016 Abdullah A. Hassan <abdullah@webtomizer.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "nexus.h"
#include "context.h"
#include "object.h"
#include "value.h"
#include "global_object.h"
//#include "globals/global.h"
#include "scoped_string.h"
#include <JavaScriptCore/API/APICast.h>
#include <JavaScriptCore/parser/SourceCode.h>
#include <JavaScriptCore/parser/UnlinkedSourceCode.h>
#include <JavaScriptCore/runtime/Completion.h>
#include <JavaScriptCore/runtime/SourceOrigin.h>
#include <JavaScriptCore/runtime/JSCInlines.h>
#include <JavaScriptCore/runtime/JSInternalPromise.h>
#include <JavaScriptCore/runtime/FunctionPrototype.h>
#include <JavaScriptCore/runtime/JSFunction.h>
#include <JavaScriptCore/runtime/JSModuleLoader.h>
#include <JavaScriptCore/API/JSCallbackFunction.h>
#include <JavaScriptCore/API/JSCallbackObject.h>
#include <JavaScriptCore/runtime/InitializeThreading.h>
#include <JavaScriptCore/parser/ParserError.h>
#include <JavaScriptCore/heap/MachineStackMarker.h>
#include "globals/global.h"
#include <JavaScriptCore/runtime/Structure.h>
#include <JavaScriptCore/runtime/StructureInlines.h>
JSValueRef NX::Context::evaluateScript(const std::string &src, JSObjectRef thisObject,
const std::string &filePath, unsigned int lineNo, JSValueRef *exception) {
NX::ScopedString srcRef(src);
NX::ScopedString filePathRef(filePath);
JSValueRef ret = JSEvaluateScript(toRef(myGlobal->globalExec()), srcRef, thisObject,
filePath.length() ? (JSStringRef) filePathRef : nullptr, lineNo, exception);
return ret;
}
JSC::JSInternalPromise * NX::Context::evaluateModule(const std::string &src, JSObjectRef thisObject,
const std::string &filePath, unsigned int lineNo) {
WTF::String code(src.c_str(), (unsigned) src.length());
WTF::String path(filePath.c_str(), (unsigned) filePath.length());
auto pos = WTF::TextPosition(WTF::OrdinalNumber::fromOneBasedInt(lineNo), OrdinalNumber());
auto source = JSC::makeSource(code,
JSC::SourceOrigin { path },
// path,
WTF::URL(),
pos, JSC::SourceProviderSourceType::Module);
auto exec = myGlobal->globalExec();
JSLockHolder lockHolder(exec);
JSC::ParserError e;
auto valid = JSC::checkModuleSyntax(exec, source, e);
if (valid) {
return JSC::loadAndEvaluateModule(exec, source, JSC::jsUndefined());
} else {
NX::Nexus::ReportSyntaxError(source, e);
return nullptr;
}
}
NX::Context::~Context() {
for(auto & val : myGlobals) {
JSValueUnprotect(toRef(myGlobal->globalExec()), val.second);
}
}
NX::Context::Context(NX::Nexus * nx, WTF::Ref<JSC::VM> vm):
myParent(nullptr), myNexus(nx), myVM(vm.leakRef()),
myGlobal(nullptr), myGlobals()
{
myNexus->scheduler()->scheduleThreadInitTask([=] { registerThread(); });
myGlobal = createGlobalObject();
}
JSC::JSGlobalObject * NX::Context::createGlobalObject() {
auto cls = myNexus->defineOrGetClass(Global::GlobalClass);
JSC::JSLockHolder locker(myVM.ptr());
auto cbStructure = JSC::JSCallbackObject<NX::GlobalObject>::createStructure(myVM, nullptr, JSC::jsNull());
auto cbGlobalObject = JSC::JSCallbackObject<NX::GlobalObject>::create(myVM, cls, cbStructure);
auto exec = cbGlobalObject->globalExec();
JSValue prototype = cls->prototype(exec);
if (!prototype)
prototype = jsNull();
cbGlobalObject->resetPrototype(myVM.get(), prototype);
cbGlobalObject->setNexus(myNexus);
cbGlobalObject->setPrivate(this);
return cbGlobalObject;
}
NX::Context *NX::Context::FromJsContext(JSContextRef pContext) {
auto exec = toJS(pContext);
auto global = exec->lexicalGlobalObject();
auto ptr = static_cast<JSCallbackObject<NX::GlobalObject>*>(global)->getPrivate();
auto context = reinterpret_cast<NX::Context*>(ptr);
// context->garbageCollect();
return context;
}
JSGlobalContextRef NX::Context::toJSContext() const {
if (myGlobal)
return toGlobalRef(myGlobal->globalExec());
return nullptr;
}
JSValueRef NX::Context::getGlobal(const char * name) {
auto search = myGlobals.find(name);
if (search == myGlobals.end()) {
return nullptr;
}
return search->second;
}
JSValueRef NX::Context::getOrInitGlobal(JSContextRef ctx, const char *name) {
auto search = myGlobals.find(name);
if (search == myGlobals.end()) {
auto exec = toJS(ctx);
JSC::JSLockHolder lock(exec);
auto ident = Identifier::fromString(exec, name);
if (myGlobal->hasOwnProperty(exec, ident)) {
auto value = myGlobal->get(exec, JSC::PropertyName(ident));
if (!value.isUndefinedOrNull()) {
auto global = toRef(myGlobal->globalExec(), value);
return setGlobal(name, global);
}
}
return nullptr;
}
return search->second;
}
JSValueRef NX::Context::setGlobal(const char * name, JSValueRef value) {
JSValueProtect(toJSContext(), value);
return myGlobals[name] = value;
}
JSObjectRef NX::Context::globalThisValue() const {
return toRef(myGlobal->globalThis());
}
std::size_t NX::Context::garbageCollect() {
JSC::VM& vm = myVM.get();;
JSC::JSLockHolder lock(vm);
vm.heap.collectNowFullIfNotDoneRecently(JSC::Sync);
return vm.heap.sizeAfterLastFullCollection();
}
void NX::Context::registerThread() {
myVM->heap.machineThreads().addCurrentThread();
}
NX::Context *NX::Context::create(NX::Nexus *nx) {
return new NX::Context(nx, JSC::VM::createContextGroup(JSC::HeapType::LargeHeap));
}
NX::Context *NX::Context::clone() {
return new NX::Context(myNexus, myVM.copyRef());
}