Skip to content

Commit b42996b

Browse files
committed
[js] Add support for loading compiled NQP code in the browser
1 parent a4f1de1 commit b42996b

File tree

1 file changed

+44
-31
lines changed

1 file changed

+44
-31
lines changed

src/vm/js/nqp-runtime/runtime.js

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -133,42 +133,55 @@ exports.libpath = function(paths) {
133133

134134
exports.loaderCtx = null;
135135

136-
op.loadbytecode = /*async*/ function(ctx, file) {
137-
// HACK - temporary hack for rakudo-js
138-
if (file == '/nqp/lib/Perl6/BOOTSTRAP.js') {
139-
file = 'Perl6::BOOTSTRAP';
140-
}
141-
142-
let loadFrom;
143-
if (ctx && ((loadFrom = ctx.lookupDynamic('$*LOADBYTECODE_FROM')) !== Null)) {
144-
} else {
145-
loadFrom = module;
146-
}
147-
148-
const oldLoaderCtx = exports.loaderCtx;
149-
exports.loaderCtx = ctx;
150-
const mangled = file.replace(/::/g, '-');
136+
/* dependencies */
137+
138+
if (process.browser) {
139+
op.loadbytecode = /*async*/ function(ctx, file) {
140+
const oldLoaderCtx = exports.loaderCtx;
141+
exports.loaderCtx = ctx;
142+
file = file.replace(/\.setting$/, '_setting');
143+
file = file.replace(/::/g, '-');
144+
require('./' + file + '.nqp-raw-runtime');
145+
exports.loaderCtx = oldLoaderCtx;
146+
};
147+
} else {
148+
op.loadbytecode = /*async*/ function(ctx, file) {
149+
// HACK - temporary hack for rakudo-js
150+
if (file == '/nqp/lib/Perl6/BOOTSTRAP.js') {
151+
file = 'Perl6::BOOTSTRAP';
152+
}
151153

152-
const prefixes = libpath.slice();
153-
prefixes.push('./', './nqp-js-on-js/');
154-
let found = false;
155-
for (const prefix of prefixes) {
156-
try {
157-
/*await*/ loadFrom.require(prefix + mangled);
154+
let loadFrom;
155+
if (ctx && ((loadFrom = ctx.lookupDynamic('$*LOADBYTECODE_FROM')) !== Null)) {
156+
} else {
157+
loadFrom = module;
158+
}
158159

159-
found = true;
160-
break;
161-
} catch (e) {
162-
if (e.code !== 'MODULE_NOT_FOUND') {
163-
throw e;
160+
const oldLoaderCtx = exports.loaderCtx;
161+
exports.loaderCtx = ctx;
162+
const mangled = file.replace(/::/g, '-');
163+
164+
const prefixes = libpath.slice();
165+
prefixes.push('./', './nqp-js-on-js/');
166+
let found = false;
167+
for (const prefix of prefixes) {
168+
try {
169+
/*await*/ loadFrom.require(prefix + mangled);
170+
171+
found = true;
172+
break;
173+
} catch (e) {
174+
if (e.code !== 'MODULE_NOT_FOUND') {
175+
throw e;
176+
}
164177
}
165178
}
166-
}
167-
if (!found) throw `can't find: ${file}, looking in: ${prefixes.join(', ')} from ${loadFrom.filename}`;
168-
exports.loaderCtx = oldLoaderCtx;
179+
if (!found) throw `can't find: ${file}, looking in: ${prefixes.join(', ')} from ${loadFrom.filename}`;
180+
exports.loaderCtx = oldLoaderCtx;
169181

170-
return file;
171-
};
182+
return file;
183+
};
184+
}
172185

173186
op.loadbytecodefh = function(ctx, fh, file) {
174187
const oldLoaderCtx = exports.loaderCtx;

0 commit comments

Comments
 (0)