Skip to content

Commit

Permalink
Merge pull request #41 from paulcuth/dev
Browse files Browse the repository at this point in the history
v0.3.0
  • Loading branch information
paulcuth committed Jul 28, 2016
2 parents 4c459ab + 4e092ea commit 37ff96f
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "starlight",
"description": "A Lua -> ES6 transpiler",
"version": "0.2.2",
"version": "0.3.0",
"author": {
"name": "Paul Cuthbertson"
},
Expand Down
3 changes: 2 additions & 1 deletion src/build-tools/common/code-generator.js
Expand Up @@ -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})`;
}
},

Expand Down
4 changes: 2 additions & 2 deletions src/parser/index.js
Expand Up @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions src/runtime/lib/globals.js
Expand Up @@ -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;
Expand Down Expand Up @@ -348,7 +354,7 @@ export function tostring(e) {
return e.hasOwnProperty('toString')? `${e}` : 'function: [host code]';
}

return coerceToString(e) || 'userdata';
return coerceToString(e);
}


Expand Down
12 changes: 9 additions & 3 deletions src/runtime/lib/string.js
Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/utils.js
Expand Up @@ -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';
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/lua/functions.lua
Expand Up @@ -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')




2 changes: 2 additions & 0 deletions test/lua/lib-require.lua
Expand Up @@ -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 ()
Expand Down
5 changes: 4 additions & 1 deletion test/lua/lib-string.lua
Expand Up @@ -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')



Expand Down
17 changes: 16 additions & 1 deletion test/lua/lib.lua
Expand Up @@ -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'

Expand All @@ -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')



Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 37ff96f

Please sign in to comment.