Permalink
Browse files

Merge pull request #29 from paulcuth/dev

v0.2.0
  • Loading branch information...
paulcuth committed Apr 10, 2016
2 parents 61205c9 + d93636e commit c3ce16d5189e9df04286c9bb2114cf0ada6afa21
View
@@ -1,7 +1,7 @@
{
"name": "starlight",
"description": "A Lua -> ES6 transpiler",
"version": "0.1.8",
"version": "0.2.0",
"author": {
"name": "Paul Cuthbertson"
},
@@ -14,7 +14,8 @@
"url": "https://github.com/paulcuth/starlight/issues"
},
"dependencies": {
"printf": "^0.2.3"
"printf": "^0.2.3",
"superagent": "^1.8.3"
},
"devDependencies": {
"babel": "^5.4.7",
@@ -1,5 +1,5 @@
# grunt-starlight
A Lua to ES6 translator
A Lua to ES6 translator
http://starlight.paulcuth.me.uk
## Getting Started
@@ -1,6 +1,6 @@
{
"name": "grunt-starlight",
"description": "A Lua -> ES6 transpiler",
"description": "A Lua to ES6 translator",
"version": "0.0.1",
"homepage": "https://github.com/paulcuth/starlight",
"author": {
View
@@ -1,5 +1,7 @@
import { getRuntimeInit, generateJS } from './code-generator';
import { default as parser } from 'luaparse';
import superagent from 'superagent';
if (typeof global === undefined) {
global = window;
@@ -27,33 +29,82 @@ function parseToString (input) {
return js;
}
function parse (input) {
return (new Function('var global = this;' + parseToString(input))).bind(global || window);
}
function runScriptTags() {
const selectors = SUPPORTED_MIME_TYPES.map(t => `script[type="${t}"]`);
const scripts = document.querySelectorAll(selectors.join());
const scriptTags = [...document.querySelectorAll(selectors.join())];
let script, i, modname, scriptBody;
let lua = '';
for (i = 0; script = scripts[i]; i++) {
modname = script.dataset.modname;
scriptBody = script.textContent;
const { immediate, deferred } = parseScriptTags(scriptTags);
runNextTag([...immediate, ...deferred]);
}
function parseScriptTags (tags) {
const immediate = [];
const deferred = [];
tags.forEach(tag => {
const src = tag.src;
if (src) {
const arr = tag.defer? deferred : immediate;
arr.push(new Promise((resolve, reject) => {
let x = superagent
.get(src)
.end((error, response) => {
if (error) {
resolve({});
} else {
resolve({
modname: tag.dataset.modname,
body: response.text
});
}
});
}));
if (modname) {
lua += " rawset(package.preload, '" + modname + "', function(...) " + scriptBody + " end) ";
} else {
lua += scriptBody;
immediate.push(Promise.resolve({
modname: tag.dataset.modname,
body: tag.textContent,
}));
}
}
});
return { immediate, deferred };
}
if (lua) {
parse(lua)();
function runNextTag (tags) {
if (!tags.length) {
return;
}
let tag;
[tag, ...tags] = tags;
tag.then(({ modname, body, error }) => {
if (body !== undefined) {
if (modname) {
body = " rawset(package.preload, '" + modname + "', function(...) " + body + " end) ";
}
parse(body)();
}
runNextTag(tags);
});
}
// Init
if (global.document && global.document.querySelector('script[src$="starlight.js"][data-run-script-tags]')) {
global.document.addEventListener('DOMContentLoaded', runScriptTags);
}
@@ -43,6 +43,9 @@ function translatePattern (pattern) {
// TODO Add support for balanced character matching (not sure this is easily achieveable).
pattern = '' + pattern;
// Replace single backslash with double backslashes
pattern = pattern.replace(new RegExp('\\\\', 'g'), '\\\\');
for (let i in ROSETTA_STONE) {
if (ROSETTA_STONE.hasOwnProperty(i)) {
pattern = pattern.replace(new RegExp(i, 'g'), ROSETTA_STONE[i]);
View
@@ -24,6 +24,14 @@ function binaryArithmetic(left, right, metaMethodName, callback) {
}
function binaryStringArithmetic(left, right, metaMethodName, callback) {
if (typeof left == 'string' && typeof right == 'string') {
return callback(left, right);
}
return binaryArithmetic(left, right, metaMethodName, callback);
}
function concat(left, right) {
let mt, f;
@@ -134,8 +142,8 @@ const op = {
},
mod: (left, right) => binaryArithmetic(left, right, '__mod', mod),
pow: (left, right) => binaryArithmetic(left, right, '__pow', Math.pow),
lt: (left, right) => binaryArithmetic(left, right, '__lt', (l, r) => l < r),
lte: (left, right) => binaryArithmetic(left, right, '__le', (l, r) => l <= r),
lt: (left, right) => binaryStringArithmetic(left, right, '__lt', (l, r) => l < r),
lte: (left, right) => binaryStringArithmetic(left, right, '__le', (l, r) => l <= r),
gt(left, right) {
return !op.lte(left, right);
View
@@ -472,7 +472,6 @@ end
-- gsub
a = '<%?xml version="1.0" encoding="UTF%-8"%?>'
@@ -514,6 +513,9 @@ assertTrue (d == ':X:X:X:X:X:', 'string.gsub() should replace the matched part o
c = string.gsub (';a;', 'a*', 'ITEM')
assertTrue (c == 'ITEM;ITEMITEM;ITEM', 'string.gsub() should replace the matched part of the string[2]')
a = 'abc\\def'
b = string.gsub(a, '\\', '\\\\')
assertEqual (b, 'abc\\\\def', 'Allow backslashes in regexes')
View
@@ -233,4 +233,5 @@ for name, test in pairs(tests) do
end
assertTrue('abc' < 'def', 'Strings should be comparable')

0 comments on commit c3ce16d

Please sign in to comment.