Skip to content

Commit

Permalink
[js] Support running browser bundled code in node.js
Browse files Browse the repository at this point in the history
Make it run also when window is not defined. This helps with debugging.
  • Loading branch information
pmurias committed Nov 27, 2018
1 parent 7fc24a9 commit 382984e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
37 changes: 20 additions & 17 deletions src/vm/js/nqp-runtime/browser.js
Expand Up @@ -9,6 +9,9 @@ const NQPObject = require('./nqp-object.js');

exports.op = op;

// allows running code bundles as for browser use outside of the browser
const maybeWindow = typeof window === 'undefined' ? {} : window;

class BufferedConsole extends NQPObject {
constructor(output) {
super();
Expand Down Expand Up @@ -76,38 +79,38 @@ class HijackedConsole extends NQPObject {
}

let STDOUT;
if (window.NQP_STDOUT) {
STDOUT = new HijackedConsole(window.NQP_STDOUT)
} else if (window.__karma__) {
if (maybeWindow.NQP_STDOUT) {
STDOUT = new HijackedConsole(maybeWindow.NQP_STDOUT)
} else if (maybeWindow.__karma__) {
const TapConsole = require('./tap-console.js');
STDOUT = new TapConsole();
} else {
STDOUT = new BufferedConsole(output => console.error(output));
}

if (window.__karma__) {
if (!window.__rakudo__) {
window.__rakudo__ = {};
if (maybeWindow.__karma__) {
if (!maybeWindow.__rakudo__) {
maybeWindow.__rakudo__ = {};
}

window.__rakudo__.startTime = new Date().getTime();
window.__rakudo__.waitForStart = [];
window.__rakudo__.results = [];
window.__rakudo__.resultCount = 0;
maybeWindow.__rakudo__.startTime = new Date().getTime();
maybeWindow.__rakudo__.waitForStart = [];
maybeWindow.__rakudo__.results = [];
maybeWindow.__rakudo__.resultCount = 0;

window.__karma__.start = function(results) {
maybeWindow.__karma__.start = function(results) {
STDOUT.karmaResults = results;

for (const code of window.__rakudo__.waitForStart) {
for (const code of maybeWindow.__rakudo__.waitForStart) {
code();
}

window.__karma__.info({total: window.__rakudo__.resultCount});
for (const result of window.__rakudo__.results) {
window.__karma__.result(result);
maybeWindow.__karma__.info({total: maybeWindow.__rakudo__.resultCount});
for (const result of maybeWindow.__rakudo__.results) {
maybeWindow.__karma__.result(result);
}
window.__karma__.complete({
coverage: window.__coverage__
maybeWindow.__karma__.complete({
coverage: maybeWindow.__coverage__
});

};
Expand Down
2 changes: 1 addition & 1 deletion src/vm/js/nqp-runtime/runtime.js
Expand Up @@ -516,7 +516,7 @@ exports.getHLL = hll.getHLL;

let once = true;
exports.run = function(code) {
if (once && browser && window.__rakudo__ && window.__rakudo__.waitForStart) {
if (once && browser && typeof window !== 'undefined' && window.__rakudo__ && window.__rakudo__.waitForStart) {
once = false;

window.__rakudo__.waitForStart.push(() => {
Expand Down

0 comments on commit 382984e

Please sign in to comment.