Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce modifications to global lua metatables #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions castl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2014,19 +2014,43 @@
compiledBinaryExpression.push(compileAdditionOperator(left, right, metaLeft, metaRight, meta));
break;
case "-":
pushSimpleBinaryExpression(compiledBinaryExpression, " - ", left, right);
if (metaLeft.type === "number" && metaRight.type === "number") {
pushSimpleBinaryExpression(compiledBinaryExpression, " - ", left, right);
} else {
compiledBinaryExpression.push("_sub(");
compiledBinaryExpression.push(left);
compiledBinaryExpression.push(",");
compiledBinaryExpression.push(right);
compiledBinaryExpression.push(")");
}
if (meta) {
meta.type = "number";
}
break;
case "*":
pushSimpleBinaryExpression(compiledBinaryExpression, " * ", left, right);
if (metaLeft.type === "number" && metaRight.type === "number") {
pushSimpleBinaryExpression(compiledBinaryExpression, " * ", left, right);
} else {
compiledBinaryExpression.push("_mul(");
compiledBinaryExpression.push(left);
compiledBinaryExpression.push(",");
compiledBinaryExpression.push(right);
compiledBinaryExpression.push(")");
}
if (meta) {
meta.type = "number";
}
break;
case "/":
pushSimpleBinaryExpression(compiledBinaryExpression, " / ", left, right);
if (metaLeft.type === "number" && metaRight.type === "number") {
pushSimpleBinaryExpression(compiledBinaryExpression, " / ", left, right);
} else {
compiledBinaryExpression.push("_div(");
compiledBinaryExpression.push(left);
compiledBinaryExpression.push(",");
compiledBinaryExpression.push(right);
compiledBinaryExpression.push(")");
}
if (meta) {
meta.type = "number";
}
Expand Down Expand Up @@ -2393,7 +2417,7 @@

function compileFunction(fun) {

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

// New context
Expand Down
8 changes: 4 additions & 4 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 = coreObjects.func(function(this, ...)
local args = pack(...)
local newArray = {}

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

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

Array.length = 1

Array.isArray = function (this, arr)
Array.isArray = coreObjects.func(function(this, arr)
return (getmetatable(arr) or {})._prototype == arrayProto
end
end)

Array.prototype = arrayProto
arrayProto.constructor = Array
Expand Down
7 changes: 4 additions & 3 deletions lua/castl/constructor/boolean.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
-- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean


local objectToString = require("castl.core_objects").objectToString
local coreObjects = require("castl.core_objects")
local booleanProto = require("castl.protos").booleanProto
local internal = require("castl.internal")

local objectToString = coreObjects.objectToString
local Boolean

local setmetatable = setmetatable
Expand All @@ -38,7 +39,7 @@ local booleanPrimitive = function(var)
end
end

Boolean = function(this, arg)
Boolean = coreObjects.func(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 +69,7 @@ Boolean = function(this, arg)
})

return o
end
end)

Boolean.prototype = booleanProto
booleanProto.constructor = Boolean
Expand Down
21 changes: 11 additions & 10 deletions lua/castl/constructor/date.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
local Date

local internal = require("castl.internal")
local coreObjects = require("castl.core_objects")
local dateparser = require("castl.modules.dateparser")
local dateProto = require("castl.protos").dateProto

Expand All @@ -30,7 +31,7 @@ local get, put, withinNew, ToNumber = internal.get, internal.put, internal.withi

_ENV = nil

Date = function(this, ...)
Date = coreObjects.func(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 +89,7 @@ Date = function(this, ...)
})

return o
end
end)

Date._timestamp = 0

Expand All @@ -105,23 +106,23 @@ if luajit then

local te = ffi.new("timeval[1]")

Date.now = function(this)
Date.now = coreObjects.func(function(this)
ffi.C.gettimeofday(te, nil);
return tonumber(te[0].tv_sec * 1000 + te[0].tv_usec / 1000);
end
end)
else
Date.now = function(this)
Date.now = coreObjects.func(function(this)
-- TODO: write a C function to get milliseconds
return time() * 1000
end
end)
end

Date.parse = function(this, str)
Date.parse = coreObjects.func(function(this, str)
-- TODO: parse RFC2822 only for now
return dateparser.parse(str, 'RFC2822') * 1000
end
end)

Date.UTC = function(this, year, month, day, hrs, min, sec, ms)
Date.UTC = coreObjects.func(function(this, year, month, day, hrs, min, sec, ms)
local timestamp = time{year=year,
month = month + 1,
day = day or 1,
Expand All @@ -132,7 +133,7 @@ Date.UTC = function(this, year, month, day, hrs, min, sec, ms)
timestamp = (timestamp + dateProto.getTimezoneOffset()) * 1000 + (ms or 0)

return timestamp
end
end)

Date.length = 7

Expand Down
5 changes: 3 additions & 2 deletions lua/castl/constructor/error/call_site.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

local CallSite

local coreObjects = require("castl.core_objects")
local callSiteProto = require("castl.protos").callSiteProto

_ENV = nil

CallSite = function(this, receiver, fun, pos, strict_mode)
CallSite = coreObjects.func(function(this, receiver, fun, pos, strict_mode)
this.receiver = receiver
this.fun = fun
this.pos = pos
end
end)

CallSite.prototype = callSiteProto
callSiteProto.constructor = CallSite
Expand Down
8 changes: 4 additions & 4 deletions lua/castl/constructor/error/error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ local ipairs, remove = ipairs, table.remove

_ENV = nil

Error = function(this, message)
Error = coreObjects.func(function(this, message)
local o = {}
o.message = message

Expand All @@ -59,9 +59,9 @@ Error = function(this, message)
Error:captureStackTrace(o, Error)

return o
end
end)

Error.captureStackTrace = function(this, error, constructorOpt)
Error.captureStackTrace = coreObjects.func(function(this, error, constructorOpt)
local frames = {}
local frame_idx = 1

Expand Down Expand Up @@ -102,7 +102,7 @@ Error.captureStackTrace = function(this, error, constructorOpt)
end
return s
end
end
end)

Error.stackTraceLimit = 10

Expand Down
5 changes: 3 additions & 2 deletions lua/castl/constructor/error/eval_error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

local EvalError

local coreObjects = require("castl.core_objects")
local Error = require("castl.constructor.error.error")
local evalErrorProto = require("castl.protos").evalErrorProto
local internal = require("castl.internal")
Expand All @@ -27,7 +28,7 @@ local get, put, ToNumber = internal.get, internal.put, internal.ToNumber

_ENV = nil

EvalError = function(this, message)
EvalError = coreObjects.func(function(this, message)
local o = {}
o.message = message

Expand All @@ -53,7 +54,7 @@ EvalError = function(this, message)
Error:captureStackTrace(o, EvalError)

return o
end
end)

EvalError.prototype = evalErrorProto
evalErrorProto.constructor = EvalError
Expand Down
5 changes: 3 additions & 2 deletions lua/castl/constructor/error/range_error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

local RangeError

local coreObjects = require("castl.core_objects")
local Error = require("castl.constructor.error.error")
local rangeErrorProto = require("castl.protos").rangeErrorProto
local internal = require("castl.internal")
Expand All @@ -27,7 +28,7 @@ local get, put, ToNumber = internal.get, internal.put, internal.ToNumber

_ENV = nil

RangeError = function(this, message)
RangeError = coreObjects.func(function(this, message)
local o = {}
o.message = message

Expand All @@ -53,7 +54,7 @@ RangeError = function(this, message)
Error:captureStackTrace(o, RangeError)

return o
end
end)

RangeError.prototype = rangeErrorProto
rangeErrorProto.constructor = RangeError
Expand Down
5 changes: 3 additions & 2 deletions lua/castl/constructor/error/reference_error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

local ReferenceError

local coreObjects = require("castl.core_objects")
local Error = require("castl.constructor.error.error")
local referenceErrorProto = require("castl.protos").referenceErrorProto
local internal = require("castl.internal")
Expand All @@ -27,7 +28,7 @@ local get, put, ToNumber = internal.get, internal.put, internal.ToNumber

_ENV = nil

ReferenceError = function(this, message)
ReferenceError = coreObjects.func(function(this, message)
local o = {}
o.message = message

Expand All @@ -53,7 +54,7 @@ ReferenceError = function(this, message)
Error:captureStackTrace(o, ReferenceError)

return o
end
end)

ReferenceError.prototype = referenceErrorProto
referenceErrorProto.constructor = ReferenceError
Expand Down
5 changes: 3 additions & 2 deletions lua/castl/constructor/error/syntax_error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

local SyntaxError

local coreObjects = require("castl.core_objects")
local Error = require("castl.constructor.error.error")
local syntaxErrorProto = require("castl.protos").syntaxErrorProto
local internal = require("castl.internal")
Expand All @@ -27,7 +28,7 @@ local get, put, ToNumber = internal.get, internal.put, internal.ToNumber

_ENV = nil

SyntaxError = function(this, message)
SyntaxError = coreObjects.func(function(this, message)
local o = {}
o.message = message

Expand All @@ -53,7 +54,7 @@ SyntaxError = function(this, message)
Error:captureStackTrace(o, SyntaxError)

return o
end
end)

SyntaxError.prototype = syntaxErrorProto
syntaxErrorProto.constructor = SyntaxError
Expand Down
5 changes: 3 additions & 2 deletions lua/castl/constructor/error/type_error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

local TypeError

local coreObjects = require("castl.core_objects")
local Error = require("castl.constructor.error.error")
local typeErrorProto = require("castl.protos").typeErrorProto
local internal = require("castl.internal")
Expand All @@ -27,7 +28,7 @@ local get, put, ToNumber = internal.get, internal.put, internal.ToNumber

_ENV = nil

TypeError = function(this, message)
TypeError = coreObjects.func(function(this, message)
local o = {}
o.message = message

Expand All @@ -53,7 +54,7 @@ TypeError = function(this, message)
Error:captureStackTrace(o, TypeError)

return o
end
end)

TypeError.prototype = typeErrorProto
typeErrorProto.constructor = TypeError
Expand Down
5 changes: 3 additions & 2 deletions lua/castl/constructor/error/uri_error.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

local URIError

local coreObjects = require("castl.core_objects")
local Error = require("castl.constructor.error.error")
local uriErrorProto = require("castl.protos").uriErrorProto
local internal = require("castl.internal")
Expand All @@ -27,7 +28,7 @@ local get, put, ToNumber = internal.get, internal.put, internal.ToNumber

_ENV = nil

URIError = function(this, message)
URIError = coreObjects.func(function(this, message)
local o = {}
o.message = message

Expand All @@ -53,7 +54,7 @@ URIError = function(this, message)
Error:captureStackTrace(o, URIError)

return o
end
end)

URIError.prototype = uriErrorProto
uriErrorProto.constructor = URIError
Expand Down
Loading