Skip to content

Commit

Permalink
Make concepts work with methods
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Feb 12, 2020
1 parent 4f0cc44 commit a3e3ca2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
14 changes: 14 additions & 0 deletions lib/myarraytable.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
local allocator = @gc_allocator
## end

local an_array_of_T = #[concept(function(x)
if x.type:is_table() then
return types.ArrayType(nil, T, #x.node[1])
elseif x.type:is_array_of(T) then
return true
end
end)]#

local T = @#[T]#
local ArrayTableImplT <codename #['nelua_ArrayTableImpl_'..T.name]#> = @record {
size: usize,
Expand Down Expand Up @@ -96,6 +104,12 @@
return self.impl.data[i]
end

function ArrayTableT:__assign(values: an_array_of_T)
self:reserve(#values)
memory.zero(&self.impl.data[0], #T)
memory.copy(&self.impl.data[1], &values[0], #values * #T)
end

function ArrayTableT:__atindex(i: usize <autocast>): T* <inline>
self:init()
if unlikely(i > self.impl.size) then
Expand Down
2 changes: 1 addition & 1 deletion nelua/analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ local function visitor_Call(context, node, argnodes, calleetype, calleesym, call
end
end

if calleeobjnode and argtype and pseudoargtypes[i]:is_auto() then
if calleeobjnode and argtype and pseudoargtypes[i].lazyable then
pseudoargtypes[i] = argtype
end
end
Expand Down
4 changes: 3 additions & 1 deletion nelua/cemitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ function CEmitter:add_cstring2string(val)
end

function CEmitter:add_val2type(type, val, valtype)
if type:is_comptime() then
if type:is_type() then
self:add('NULL')
return
end

assert(not type:is_comptime())

if not valtype and traits.is_astnode(val) then
valtype = val.attr.type
end
Expand Down
16 changes: 12 additions & 4 deletions spec/05-cgenerator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1886,28 +1886,36 @@ end)
it("concepts", function()
assert.run_c([=[
## strict = true
local an_array = #[concept(function(attr)
if attr.type and attr.type:is_array() then
return true
end
end)]#
local an_arithmetic = #[concept(function(attr)
if attr.type:is_arithmetic() then
return true
end
end)]#
local function f(a: an_array, x: an_arithmetic, len: integer)
## print(a.type)
assert(a[0] == x)
end
local a: integer[4] = {1,2,3,4}
local b: number[3] = {5,6,7}
f(a, a[0], #a)
f(b, b[0], #b)
local R = @record {
x: integer
}
function R:__assign(x: an_arithmetic)
self.x = x
end
local r: R
R.__assign(&r, 1)
assert(r.x == 1)
r:__assign(2)
assert(r.x == 2)
]=])
end)

Expand Down

0 comments on commit a3e3ca2

Please sign in to comment.