Permalink
Browse files

Merge pull request #41 from paulcuth/dev

v0.3.0
  • Loading branch information...
paulcuth committed Jul 28, 2016
2 parents 4c459ab + 4e092ea commit 37ff96ff62af4f8d8e704d584594852f29f00bfb
View
@@ -1,7 +1,7 @@
{
"name": "starlight",
"description": "A Lua -> ES6 transpiler",
- "version": "0.2.2",
+ "version": "0.3.0",
"author": {
"name": "Paul Cuthbertson"
},
@@ -215,7 +215,8 @@ const GENERATORS = {
} else if (isMemberExpr) {
return identifier.set(funcDef);
} else {
- return `$set($, '${identifier}', ${funcDef})`;
+ const local = node.isLocal ? 'Local' : '';
+ return `$set${local}($, '${identifier}', ${funcDef})`;
}
},
View
@@ -96,9 +96,9 @@ function runNextTag (tags) {
tag.then(({ modname, body, error }) => {
if (body !== undefined) {
if (modname) {
- body = " rawset(package.preload, '" + modname + "', function(...) " + body + " end) ";
+ body = ` rawset(package.preload, '${modname}', function(...) ${body} end) `;
}
- parse(body)();
+ parse(body)();
}
runNextTag(tags);
View
@@ -239,13 +239,19 @@ export function _require(modname) {
modname = modname.replace(/\//g, '.');
let [preload, loaded] = getPackageMethods();
+ let mod = loaded.rawget(modname);
+
+ if (mod) {
+ return mod;
+ }
+
let modinit = preload.rawget(modname);
if (modinit === void 0) {
throw new LuaError(`module '${modname}' not found:\n\tno field package.preload['${modname}']`);
}
- let mod = modinit(modname)[0];
+ mod = modinit(modname)[0];
loaded.rawset(modname, mod !== void 0 ? mod : true);
return mod;
@@ -348,7 +354,7 @@ export function tostring(e) {
return e.hasOwnProperty('toString')? `${e}` : 'function: [host code]';
}
- return coerceToString(e) || 'userdata';
+ return coerceToString(e);
}
View
@@ -198,12 +198,18 @@ export function gsub(s, pattern, repl, n = Infinity) {
} else {
str = `${repl}`.replace(/%([0-9])/g, (m, i) => match[i]);
+
}
- if (match[0].length === 0 && lastMatch === void 0) {
- prefix = '';
+ if (match[0].length === 0) {
+ if (lastMatch === void 0) {
+ prefix = '';
+ } else {
+ prefix = s.substr(0, 1);
+ }
+
} else {
- prefix = s.split(match[0], 1)[0];
+ prefix = s.substr(0, match.index);
}
lastMatch = match[0];
View
@@ -133,7 +133,7 @@ export function coerceToString(val, errorMessage) {
return global.isNaN(val)? 'nan' : `${val}`;
default:
- return throwCoerceError(val, errorMessage) || '';
+ return throwCoerceError(val, errorMessage) || 'userdata';
}
}
View
@@ -107,3 +107,23 @@ x = addThree (4)
assertTrue (x == 7, 'Functions should be able to be curried')
+do
+ local function x()
+ return 'inner'
+ end
+
+ function y()
+ return x()
+ end
+end
+
+function x()
+ return 'outer'
+end
+
+local z = y()
+assertTrue (z == 'inner', 'Local functions should be locally scoped')
+
+
+
+
View
@@ -44,6 +44,8 @@ assertTrue(type(sub2) == 'table', 'Module should be able to load more modules us
mainGlobal1 = 'innerGlbl'
local innerLocal = 'innerLoc'
+moduleInitCount = moduleInitCount + 1
+
return {
getValue = function ()
View
@@ -515,8 +515,11 @@ assertTrue (c == 'ITEM;ITEMITEM;ITEM', 'string.gsub() should replace the matched
a = 'abc\\def'
b = string.gsub(a, '\\', '\\\\')
-assertEqual (b, 'abc\\\\def', 'Allow backslashes in regexes')
+assertEqual (b, 'abc\\\\def', 'string.gsub() should allow backslashes')
+a = "a = 'a', b = 'b', c = 'c',"
+b = string.gsub(a, ",$", "")
+assertEqual (b, "a = 'a', b = 'b', c = 'c'", 'string.gsub() should match $ with end of string')
View
@@ -313,6 +313,7 @@ assertTrue (c == nil, 'pcall() should only return 2 items when the function erro
mainGlobal1 = 'mainGlbl'
mainGlobal2 = 'mainGlbl'
+moduleInitCount = 0
local mainLocal = 'mainLoc'
@@ -324,11 +325,14 @@ assertTrue (result.getValue() == 'modVal', 'require() should return the value th
assertTrue (package.loaded['lib-require'] == result, 'Module loaded by require() should also be available in package.loaded[modname]')
+assertTrue (moduleInitCount == 1, 'require() should initialise module')
assertTrue (mainGlobal1 == 'innerGlbl', 'require() should pass the same global namespace into the module[1]')
assertTrue (mainGlobal2 == 'mainGlbl', 'require() should pass the same global namespace into the module[2]')
assertTrue (innerLocal == nil, 'Module locals should not leak into outer environment in a require() call')
-
+local result2 = require 'lib-require'
+assertTrue (moduleInitCount == 1, 'require() should only initialise a module once')
+assertTrue (result == result2, 'require() should return the same value across multiple calls')
@@ -527,6 +531,17 @@ assertTrue (f == '-inf', 'tostring() should convert negative infinity to "-inf"'
assertTrue (g == 'nan', 'tostring() should convert not-a-number to "nan"')
assertTrue (h == 'true', 'tostring() should convert a boolean to a string')
+a = tostring ('')
+b = tostring ('moo')
+c = tostring (0)
+d = tostring (false)
+
+assertTrue (a == '', 'tostring() should convert a zero-length string to a string')
+assertTrue (b == 'moo', 'tostring() should convert a non-zero-length string to a string')
+assertTrue (c == '0', 'tostring() should convert zero to a string')
+assertTrue (d == 'false', 'tostring() should convert false to a string')
+
+
a = {}
setmetatable(a, { __tostring = function () return 'Les Revenants' end })
b = tostring (a)

0 comments on commit 37ff96f

Please sign in to comment.