Permalink
Browse files

Merge pull request #40 from paulcuth/dev

v0.2.2
  • Loading branch information...
paulcuth committed Jul 19, 2016
2 parents 8c30f84 + f9c2e51 commit 4c459abf66278e64fd1b768b661b060384e7b2f2
View
@@ -1,7 +1,7 @@
{
"name": "starlight",
"description": "A Lua -> ES6 transpiler",
"version": "0.2.1",
"version": "0.2.2",
"author": {
"name": "Paul Cuthbertson"
},
View
@@ -36,7 +36,7 @@
children = Object.getOwnPropertyNames(property);
for (i = 0; child = children[i]; i++) {
if (child == 'caller' || child == 'callee' || child == 'arguments') continue; // Avoid issues in strict mode. Fixes mooshine issue #24.
if (child == 'caller' || child == 'callee' || child == 'arguments') continue; // Avoid issues in strict mode. Fixes moonshine issue #24.
f[child] = property[child];
}
}
@@ -90,13 +90,6 @@
function luaToJS (val) {
var mt;
// Make shine.Functions invokable
if (val instanceof Function) {
return function () {
return jsToLua(val.apply(void 0, convertArguments(arguments, jsToLua)));
};
}
if (val instanceof T) {
// If object has been wrapped by jsToLua(), use original object instead
if ((mt = val.metatable) && mt.source) {
@@ -151,19 +144,25 @@
// Add extact method
win.set('extract', function () {
var val, i;
for (i in window) {
if (i !== 'print' && i !== 'window' && win[i] !== null) {
try {
val = _G.get('window').get(i);
} catch(e) {
val = function () {
throw new starlight.runtime.LuaError('error accessing property: ' + e.message);
}
}
_G.set(i, typeof val == 'function' ? val.bind(void 0, window) : val);
}
}
var obj = window;
do {
Object.getOwnPropertyNames(obj).forEach(function (key) {
if (key !== 'print' && key !== 'window' && win[i] !== null) {
try {
val = _G.get('window').get(key);
} catch (e) {
val = function () {
throw new starlight.runtime.LuaError('error accessing property: ' + e.message);
}
}
_G.set(key, typeof val == 'function' ? val.bind(void 0, window) : val);
}
});
} while (obj = Object.getPrototypeOf(obj));
});
@@ -34,8 +34,6 @@ const BIN_OP_MAP = {
const GENERATORS = {
AssignmentStatement(node, scope) {
let canOptimise = true;
let assignments = node.variables.map((variable, index) => {
let name;
name = scoped(variable, scope);
@@ -55,17 +53,12 @@ const GENERATORS = {
let values = node.init.map((init, index) => {
let value = scoped(init, scope);
if (isCallExpression(init)) {
canOptimise = false;
return `...${value}`;
}
return value;
});
if (canOptimise) {
return assignments.replace(/__star_tmp\[(\d+)\]/g, (match, index) => values[index]);
} else {
return `__star_tmp = [${values.join(', ')}];${assignments}`;
}
return `__star_tmp = [${values.join(', ')}];${assignments}`;
},
View
@@ -1,3 +1,5 @@
const hasOwnProperty = Object.prototype.hasOwnProperty;
export default class Scope {
constructor(variables = {}) {
this._variables = variables;
@@ -10,7 +12,7 @@ export default class Scope {
set(key, value) {
let vars = this._variables;
if (this._variables.hasOwnProperty(key) || !this.parent) {
if (hasOwnProperty.call(this._variables, key) || !this.parent) {
vars[key] = value;
} else {
this.parent.set(key, value);
View
@@ -1,12 +1,13 @@
import { default as LuaError } from './LuaError';
import { type } from './lib/globals';
let count = 0;
let stringLib;
let stringLib, getn;
export function registerStringLib(lib) {
stringLib = lib; // Can't import it directly because that'll create a circular dependency. :(
export function registerLibs(libs) {
// Can't import directly because they'll create a circular dependencies. :(
stringLib = libs.string;
getn = libs.getn;
};
@@ -46,7 +47,10 @@ export default class Table {
if (type(this) == 'string') {
return stringLib.get(key);
} else if (type(this) == 'userdata') {
} else if (
type(this) === 'userdata'
|| (type(this) === 'function' && key === 'new') // exception for DOMAPI compat with Moonshine
) {
if (key in this) {
return this[key];
}
@@ -77,7 +81,7 @@ export default class Table {
rawget(key) {
switch (typeof key) {
case 'string': return this.strValues[key];
case 'string': return Object.prototype.hasOwnProperty.call(this.strValues, key) ? this.strValues[key] : void 0;
case 'number':
if (key > 0 && key == key >> 0) {
return this.numValues[key];
@@ -181,4 +185,30 @@ export default class Table {
return 'table: 0x' + this.index.toString(16);
}
}
toObject() {
const isArr = getn(this) > 0;
const result = isArr? [] : {};
const numValues = this.numValues;
const strValues = this.strValues;
let i;
const l = numValues.length;
for (i = 1; i < l; i++) {
const propValue = numValues[i];
result[i - 1] = (propValue instanceof Table)? propValue.toObject() : propValue;
}
for (i in strValues) {
if (strValues.hasOwnProperty(i)) {
const propValue = strValues[i];
result[i] = (propValue instanceof Table)? propValue.toObject() : propValue;
}
}
return result;
}
};
View
@@ -1,7 +1,7 @@
import { default as Scope } from './Scope';
import { default as globals, type } from './lib/globals';
import { default as operators } from './operators';
import { default as Table, registerStringLib } from './Table';
import { default as Table, registerLibs } from './Table';
import { default as LuaError } from './LuaError';
@@ -62,17 +62,17 @@ let runtime = namespace.runtime = {
import { default as math } from './lib/math';
_G.set('math', math);
import { default as table } from './lib/table';
import { default as table, getn } from './lib/table';
_G.set('table', table);
import { default as string } from './lib/string';
_G.set('string', string);
registerStringLib(string);
import { default as os } from './lib/os';
_G.set('os', os);
import { default as _package } from './lib/package';
_G.set('package', _package);
registerLibs({ string, getn });
@@ -1,7 +1,4 @@
import { default as T } from '../Table';
// import { default as LuaError } from '../LuaError';
// import { coerceArgToString } from '../utils';
export default new T({
View
@@ -30,6 +30,11 @@
local a = 1
assertTrue (a == 1, 'Local should retain value')
local a, b = 12, 34
a, b = b, a
assertTrue (a == 34, 'Assignment should be able to reverse values [1]')
assertTrue (b == 12, 'Assignment should be able to reverse values [2]')
local a, b, c, d = 5, 20, 0, nil
assertTrue (a == 5, 'Local should change value')
assertTrue (b == 20, 'Local should accept multiple assignments')

0 comments on commit 4c459ab

Please sign in to comment.