Skip to content

Commit

Permalink
Don't mutate lua's function metatable
Browse files Browse the repository at this point in the history
  • Loading branch information
daurnimator committed Mar 27, 2017
1 parent 7d6f6c5 commit 6fb45f4
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 85 deletions.
2 changes: 1 addition & 1 deletion castl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2393,7 +2393,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
5 changes: 3 additions & 2 deletions lua/castl/constructor/function.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local esprima, castl, runtime
local luajit = jit ~= nil

local errorHelper = require("castl.modules.error_helper")
local coreObjects = require("castl.core_objects")
local throw = require("castl.jssupport").throw
local functionProto = require("castl.protos").functionProto

Expand All @@ -30,7 +31,7 @@ local require, assert, load, tinsert, concat = require, assert, load, table.inse

_ENV = nil

Function = function(this, ...)
Function = coreObjects.func(function(this, ...)
if ... then
local args = pack(...)
local body = args[args.n]
Expand Down Expand Up @@ -74,7 +75,7 @@ Function = function(this, ...)
end

return function () end
end
end)

Function.prototype = functionProto
functionProto.constructor = Function
Expand Down
Loading

0 comments on commit 6fb45f4

Please sign in to comment.