Skip to content

Commit

Permalink
https://github.com/PaulBernier/castl/issues/3
Browse files Browse the repository at this point in the history
  • Loading branch information
chriku committed Dec 30, 2018
1 parent 7d6f6c5 commit 3e97a11
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 26 deletions.
11 changes: 6 additions & 5 deletions castl.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}(this, function (exports) {
"use strict";

var luaKeywords = ['and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function', 'goto', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat', 'return', 'then', 'true', 'until', 'while'];
var luaKeywords = ['and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function', 'goto', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat', 'return', 'then', 'true', 'until', 'while','newwrap'];

var options, annotations;
var labelTracker = [];
Expand Down Expand Up @@ -2393,7 +2393,7 @@

function compileFunction(fun) {

var compiledFunction = ["(function ("];
var compiledFunction = ["(newwrap(function ("];
var compiledBody = "";

// New context
Expand Down Expand Up @@ -2462,7 +2462,7 @@
// Append body and close function
compiledFunction.push(compiledBody);
compiledFunction.push("\n");
compiledFunction.push("end)");
compiledFunction.push("end))");

return compiledFunction.join('');
}
Expand Down Expand Up @@ -2620,20 +2620,21 @@
switch (typeof (literal.value)) {
case "string":
// @string
ret = '"' + sanitizeLiteralString(literal.value) + '"';
ret = 'newwrap("' + sanitizeLiteralString(literal.value) + '")';
if (meta) {
meta.type = "string";
}
break;
case "number":
// JSON.stringify write numbers in base 10
// (Lua doesn't handle octal notation)
ret = JSON.stringify(literal.value);
ret = 'newwrap('+JSON.stringify(literal.value)+')';
if (meta) {
meta.type = "number";
}
break;
case "boolean":
ret='newwrap('+literal.value+')';
if (meta) {
meta.type = "boolean";
}
Expand Down
4 changes: 2 additions & 2 deletions lua/castl/constructor/array.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ local pack = table.pack or function(...) return {n = select('#',...),...} end

_ENV = nil

Array = function (this, ...)
Array = newwrap(function (this, ...)
local args = pack(...)
local newArray = {}

Expand All @@ -40,7 +40,7 @@ Array = function (this, ...)
end

return coreObjects.array(newArray, newArray.length)
end
end)

Array.length = 1

Expand Down
4 changes: 2 additions & 2 deletions lua/castl/constructor/boolean.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ local booleanPrimitive = function(var)
end
end

Boolean = function(this, arg)
Boolean = newwrap(function(this, arg)
-- Boolean constructor not called within a new
if not withinNew(this, booleanProto) then
return booleanPrimitive(arg)
Expand Down Expand Up @@ -68,7 +68,7 @@ Boolean = function(this, arg)
})

return o
end
end)

Boolean.prototype = booleanProto
booleanProto.constructor = Boolean
Expand Down
4 changes: 2 additions & 2 deletions lua/castl/constructor/date.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ local get, put, withinNew, ToNumber = internal.get, internal.put, internal.withi

_ENV = nil

Date = function(this, ...)
Date = newwrap(function(this, ...)
-- Date constructor not called within a new
if not withinNew(this, dateProto) then
return date("%a %h %d %Y %H:%M:%S GMT%z (%Z)")
Expand Down Expand Up @@ -88,7 +88,7 @@ Date = function(this, ...)
})

return o
end
end)

Date._timestamp = 0

Expand Down
6 changes: 3 additions & 3 deletions lua/castl/constructor/function.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ local require, assert, load, tinsert, concat = require, assert, load, table.inse

_ENV = nil

Function = function(this, ...)
Function = newwrap(function(this, ...)
if ... then
local args = pack(...)
local body = args[args.n]
Expand Down Expand Up @@ -73,8 +73,8 @@ Function = function(this, ...)
return compiledFunction
end

return function () end
end
return newwrap(function () end)
end)

Function.prototype = functionProto
functionProto.constructor = Function
Expand Down
4 changes: 2 additions & 2 deletions lua/castl/constructor/number.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ local get, put, withinNew, ToNumber = internal.get, internal.put, internal.withi

_ENV = nil

Number = function(this, arg)
Number = newwrap(function(this, arg)
arg = ToNumber(arg)
-- Number constructor not called within a new
if not withinNew(this, numberProto) then
Expand Down Expand Up @@ -61,7 +61,7 @@ Number = function(this, arg)
})

return o
end
end)

Number.isFinite = function(this, arg)
if type(arg) == 'number' then
Expand Down
4 changes: 2 additions & 2 deletions lua/castl/constructor/object.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ local getFunctionProxy = internal.getFunctionProxy

_ENV = nil

Object = function (this, obj)
Object = newwrap(function (this, obj)
if obj == nil or obj == null then
return coreObjects.obj({})
end

return ToObject(obj)
end
end)

Object.create = function (this, prototype, props)
-- get function proxy
Expand Down
4 changes: 2 additions & 2 deletions lua/castl/constructor/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ local ToString, withinNew, get, put, ToNumber, ToUint16 = internal.ToString, int

_ENV = nil

String = function(this, ...)
String = newwrap(function(this, ...)
local arg = pack(...).n > 0 and ToString(...) or "";

-- String constructor not called within a new
Expand Down Expand Up @@ -67,7 +67,7 @@ String = function(this, ...)
})

return o
end
end)

local toUTF8Array = function (charcode)
local utf8 = {}
Expand Down
40 changes: 34 additions & 6 deletions lua/castl/core_objects.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ functionMt.__div = function(a, b)
return ToNumber(a) / ToNumber(b)
end

debug.setmetatable((function () end), functionMt)

--[[
Array metatable
Expand Down Expand Up @@ -233,7 +232,6 @@ booleanMt.__div = function(a, b)
return ToNumber(a) / ToNumber(b)
end

debug.setmetatable(true, booleanMt)

--[[
Number metatable
Expand All @@ -246,7 +244,6 @@ end
-- immutable
numberMt.__newindex = function() end

debug.setmetatable(0, numberMt)

--[[
String metatable
Expand Down Expand Up @@ -286,13 +283,44 @@ stringMt.__div = function(a, b)
return ToNumber(a) / ToNumber(b)
end

debug.setmetatable("", stringMt)
local gmt={}
local map=setmetatable({},{__mode="k"})

local others={__index=1,__newindex=1,__sub=0,__mul=0,__div=0,__tostring=1}
for k,tp in pairs(others) do
gmt[k]=function(self,...)
local item=map[self]
if not item then return end
local mt
if item.ltype=="string" then mt=stringMt[k] end
if item.ltype=="number" then mt=numberMt[k] end
if item.ltype=="boolean" then mt=booleanMt[k] end
if item.ltype=="function" then mt=functionMt[k] end
if not mt then else return end
if tp==0 then
return mt(...)
elseif tp==1 then
return mt(unwrap(self),...)
end
end
end

function newwrap(value)
if map[value] then return value end
local ret={}
map[ret]={type=typeof(type(value)),ltype=type(value),value=value}
return setmetatable(ret,gmt)
end
function unwrap(value)
return (map[value] and map[value].value) or value
end


--[[
Nil metatable
--]]

undefinedMt.__index = function(self, key)
--[[undefinedMt.__index = function(self, key)
throw(errorHelper.newTypeError("Cannot read property '" .. key .. "' of undefined"))
end
Expand All @@ -314,7 +342,7 @@ undefinedMt.__div = function ()
return 0/0
end
debug.setmetatable(nil, undefinedMt)
debug.setmetatable(nil, undefinedMt)]]

--[[
Support functions
Expand Down
1 change: 1 addition & 0 deletions runTests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
console.log("TEST");

/*
Copyright (c) 2014, Paul Bernier
Expand Down

0 comments on commit 3e97a11

Please sign in to comment.