Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[js] Unbitrot webpack support.
Using a setting, wrapping js values and making webpack serve source maps still NYI.
  • Loading branch information
pmurias committed Feb 10, 2016
1 parent d652e27 commit afe4963
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/webpacked/README
@@ -1,5 +1,5 @@
You need to install webpack.
And then using npm install src/vm/js/nqp-loader and src/vm/js/nqp-runtime.
And then using npm install src/vm/js/nqp-loader and src/vm/js/nqp-runtime and install empty-module from npm.
For increased awesomeness we use webpack-dev-server and webpack-reload-plugin so you need to install those.


Expand Down
23 changes: 14 additions & 9 deletions examples/webpacked/example.nqp
@@ -1,12 +1,17 @@
my &js := nqp::getcomp("JavaScript");
my &set-draw := js('(function($CTX, $NAMED, cb) {draw = cb})');
my &set-setup := js('(function($CTX, $NAMED, cb) {setup = cb})');
my &fill := js('(function($CTX, $NAMED, color) {fill(color)})');
my &ellipse := js('(function($CTX, $NAMED, a, b, c, d) {ellipse(a, b, c, d)})');
my &createCanvas := js('(function($CTX, $NAMED, width, height) {createCanvas(width, height)})');
my &mouseX := js('(function($CTX, $NAMED) {return mouseX})');
my &mouseY := js('(function($CTX, $NAMED) {return mouseY})');
my &mouseIsPressed := js('(function($CTX, $NAMED) {return mouseIsPressed ? 1 : 0})');
sub js($code) {
nqp::getcomp("JavaScript").eval($code);
}
js('window.wrap = function(func) {return {"$call": func}}');
js('window.unwrap = function(code_ref) {return function() {code_ref.$call(null, {})}}');

my &set-draw := js('wrap(function($CTX, $NAMED, cb) {window.draw = unwrap(cb)})');
my &set-setup := js('wrap(function($CTX, $NAMED, cb) {window.setup = unwrap(cb)})');
my &fill := js('wrap(function($CTX, $NAMED, color) {fill(color.value)})');
my &ellipse := js('wrap(function($CTX, $NAMED, a, b, c, d) {ellipse(a, b, c.value, d.value)})');
my &createCanvas := js('wrap(function($CTX, $NAMED, width, height) {createCanvas(width.value, height.value)})');
my &mouseX := js('wrap(function($CTX, $NAMED) {return mouseX})');
my &mouseY := js('wrap(function($CTX, $NAMED) {return mouseY})');
my &mouseIsPressed := js('wrap(function($CTX, $NAMED) {return mouseIsPressed ? 1 : 0})');

sub setup() {
createCanvas(640, 480);
Expand Down
11 changes: 11 additions & 0 deletions examples/webpacked/webpack.config.js
Expand Up @@ -3,6 +3,17 @@ module.exports = {
entry: "./example.nqp",
nqpRepo: "../..", // Edit this to specify where the nqp-js repo is
// plugins: [new ReloadPlugin("localhost")],
resolve: {
alias: {
"ffi": "empty-module",
"fs-ext": "empty-module",
"bignum": "empty-module",
"ffi": "empty-module",
"ref": "empty-module",
"nqp-js-io": "empty-module",
"sleep": "empty-module"
}
},
output: {
path: __dirname,
filename: "bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion src/vm/js/Chunk.nqp
Expand Up @@ -46,7 +46,7 @@ class Chunk {
my @parts;
for @!setup -> $part {
if nqp::isstr($part) {
nqp::push(@parts,quote_string($part));
nqp::push(@parts,quote_string($part, :json));
} else {
nqp::push(@parts,$part.with_source_map_info);
}
Expand Down
4 changes: 2 additions & 2 deletions src/vm/js/Utils.nqp
@@ -1,7 +1,7 @@
use QASTNode;

# turn a string into a javascript literal
sub quote_string($str) {
sub quote_string($str, :$json) {
my $out := '';
my $quoted := nqp::escape($str);

Expand All @@ -15,7 +15,7 @@ sub quote_string($str) {
} elsif $backslash && $c eq 'a' {
$out := $out ~ 'x07';
} elsif $backslash && $c eq 'n' {
$out := $out ~ "n\\\n";
$out := $out ~ ($json ?? "n" !! "n\\\n");
} else {
if ($c eq "\x[2028]") {
$out := $out ~ "\\u2028";
Expand Down
8 changes: 4 additions & 4 deletions src/vm/js/nqp-loader/main.js
@@ -1,8 +1,8 @@
var sh = require('execSync');
var temp = require('temp');
var fs = require('fs');
var map = require('source-map');
var loaderUtils = require("loader-utils");
var child_process = require('child_process');

var SourceNode = map.SourceNode;
var p6source;
Expand Down Expand Up @@ -31,12 +31,12 @@ module.exports = function(source) {
var tmp = temp.openSync();
fs.writeFileSync(tmp.path, source);

var command = "cd "+path+";.//nqp-p " + "src/vm/js/bin/nqp-js.nqp --source-map '" + tmp.path + "'";
var command = "cd "+path+";.//nqp-js --setting=NULL --target=js --source-map '" + tmp.path + "'";


var result = sh.exec(command);
var result = child_process.execSync(command, {encoding:"utf8"});

var data = JSON.parse(result.stdout);
var data = JSON.parse(result);

p6source = nqpRequest;
var node = new SourceNode(1,0, p6source, toNode(data));
Expand Down
1 change: 0 additions & 1 deletion src/vm/js/nqp-loader/package.json
Expand Up @@ -8,7 +8,6 @@
"main":"main.js",
"dependencies":{
"temp": "*",
"execSync": "*",
"source-map": "*",
"loader-utils": "0.2.x"
}
Expand Down

0 comments on commit afe4963

Please sign in to comment.