Skip to content

Commit 8bfba76

Browse files
committed
codegen: compile equivalent and metatable_is inline
1 parent ac91a4f commit 8bfba76

5 files changed

Lines changed: 81 additions & 4 deletions

File tree

‎spec/codegen_spec.moon‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ describe "tableshape.codegen", ->
6767
"range string": types.range "a", "f"
6868
"pattern": types.pattern "^%d+$"
6969
"custom": types.custom (v) -> type(v) == "table"
70-
"equivalent (fallback)": types.equivalent {1, 2, {3}}
70+
"equivalent table": types.equivalent {1, 2, {3}}
71+
"equivalent string": types.equivalent "hello"
72+
"equivalent number": types.equivalent 5
73+
"metatable_is table type": types.metatable_is types.table
74+
"metatable_is nil type": types.metatable_is types["nil"]
7175

7276
"transform": types.string / (s) -> "<#{s}>"
7377
"transform constant": types.string / "CONST"
@@ -242,7 +246,8 @@ describe "tableshape.codegen", ->
242246
"transform function": types.string / (s) -> s
243247
"function tag": types.string\tag (state, v) -> nil
244248
"table literal": types.literal {1, 2}
245-
"fallback type": types.equivalent {1, 2}
249+
"equivalent table": types.equivalent {1, 2}
250+
"fallback type": types.assert types.string
246251
}
247252

248253
for name, t in pairs cases
@@ -777,3 +782,38 @@ describe "tableshape.codegen large types", ->
777782
value, state = mod.transform input
778783
assert.same "v1", state.f1
779784
assert.same "v250", state.f250
785+
786+
describe "tableshape.codegen metatable_is", ->
787+
mt = { __name: "thing" }
788+
other_mt = { __name: "other" }
789+
790+
it "matches a literal metatable", ->
791+
t = types.metatable_is mt
792+
compiled = compile t
793+
794+
assert_parity t, compiled, setmetatable {}, mt
795+
assert_parity t, compiled, setmetatable {}, other_mt
796+
assert_parity t, compiled, {}
797+
assert_parity t, compiled, "hello"
798+
799+
it "matches a metatable through a shape", ->
800+
t = types.metatable_is types.shape { __name: types.string }
801+
compiled = compile t
802+
803+
assert_parity t, compiled, setmetatable {}, mt
804+
assert_parity t, compiled, setmetatable {}, { __name: 5 }
805+
assert_parity t, compiled, {}
806+
807+
it "fails when the metatable type changes the metatable", ->
808+
t = types.metatable_is types.literal(mt) / other_mt
809+
compiled = compile t
810+
assert_parity t, compiled, setmetatable {}, mt
811+
812+
it "updates the metatable with allow_metatable_update", ->
813+
t = types.metatable_is types.literal(mt) / other_mt, allow_metatable_update: true
814+
compiled = compile t
815+
816+
input = setmetatable {}, mt
817+
value = assert compiled\transform input
818+
assert rawequal(input, value)
819+
assert.equal other_mt, getmetatable value

‎tableshape/codegen.lua‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ local ArrayContains = types.array_contains
8787
local MapOf = types.map_of
8888
local Range = types.range
8989
local Custom = types.custom
90+
local Equivalent = types.equivalent
91+
local MetatableIsType = types.metatable_is
9092
local TagScopeType = types.scope
9193
local Proxy = types.proxy
9294
local AnnotateNode = types.annotate
@@ -350,8 +352,14 @@ do
350352
end
351353
end
352354
local _exp_0 = node.__class
353-
if AnyType == _exp_0 or Literal == _exp_0 or Range == _exp_0 or Custom == _exp_0 or ArrayType == _exp_0 then
355+
if AnyType == _exp_0 or Literal == _exp_0 or Range == _exp_0 or Custom == _exp_0 or ArrayType == _exp_0 or Equivalent == _exp_0 then
354356
return true, false
357+
elseif MetatableIsType == _exp_0 then
358+
if node.allow_metatable_update then
359+
return false, false
360+
else
361+
return self:is_pure(node.metatable_type)
362+
end
355363
elseif Pattern == _exp_0 then
356364
return true, false
357365
elseif NotType == _exp_0 then
@@ -457,6 +465,15 @@ do
457465
end
458466
elseif Custom == _exp_0 then
459467
return "(" .. tostring(self:ref(node.fn)) .. "(" .. tostring(v) .. ", " .. tostring(s) .. "))"
468+
elseif Equivalent == _exp_0 then
469+
if type(node.val) == "table" then
470+
return tostring(self:ref(Equivalent.values_equivalent)) .. "(" .. tostring(self:ref(node.val)) .. ", " .. tostring(v) .. ")"
471+
else
472+
return tostring(v) .. " == " .. tostring(self:value_expr(node.val))
473+
end
474+
elseif MetatableIsType == _exp_0 then
475+
local inner = self:predicate_expr(node.metatable_type, "getmetatable(" .. tostring(v) .. ")", s)
476+
return "(type(" .. tostring(v) .. ") == 'table' and " .. tostring(inner) .. ")"
460477
elseif OneOf == _exp_0 then
461478
if node.options_hash then
462479
return tostring(self:const(node.options_hash)) .. "[" .. tostring(v) .. "] ~= nil"

‎tableshape/codegen.moon‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ ArrayContains = types.array_contains
152152
MapOf = types.map_of
153153
Range = types.range
154154
Custom = types.custom
155+
Equivalent = types.equivalent
156+
MetatableIsType = types.metatable_is
155157
TagScopeType = types.scope
156158
Proxy = types.proxy
157159
AnnotateNode = types.annotate
@@ -430,8 +432,15 @@ class Compiler
430432
return (custom and true or false), false
431433

432434
switch node.__class
433-
when AnyType, Literal, Range, Custom, ArrayType
435+
when AnyType, Literal, Range, Custom, ArrayType, Equivalent
434436
true, false
437+
when MetatableIsType
438+
-- with allow_metatable_update the metatable type's result is written
439+
-- back to the value
440+
if node.allow_metatable_update
441+
false, false
442+
else
443+
@is_pure node.metatable_type
435444
when Pattern
436445
-- a coerce type only produces the test value, the input passes
437446
-- through untouched
@@ -522,6 +531,14 @@ class Compiler
522531
"(type(#{v}) == 'string' and string_match(#{v}, #{pat}) ~= nil)"
523532
when Custom
524533
"(#{@ref node.fn}(#{v}, #{s}))"
534+
when Equivalent
535+
if type(node.val) == "table"
536+
"#{@ref Equivalent.values_equivalent}(#{@ref node.val}, #{v})"
537+
else
538+
"#{v} == #{@value_expr node.val}"
539+
when MetatableIsType
540+
inner = @predicate_expr node.metatable_type, "getmetatable(#{v})", s
541+
"(type(#{v}) == 'table' and #{inner})"
525542
when OneOf
526543
if node.options_hash
527544
"#{@const node.options_hash}[#{v}] ~= nil"

‎tableshape/init.lua‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,7 @@ do
19871987
return false
19881988
end
19891989
end
1990+
self.values_equivalent = values_equivalent
19901991
if _parent_0.__inherited then
19911992
_parent_0.__inherited(_parent_0, _class_0)
19921993
end

‎tableshape/init.moon‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,8 @@ class Equivalent extends BaseType
970970
else
971971
false
972972

973+
@values_equivalent: values_equivalent
974+
973975
new: (@val) =>
974976

975977
_describe: =>

0 commit comments

Comments
 (0)