Skip to content

Commit

Permalink
making push/enq/insert do the conversion before growing, so you don't…
Browse files Browse the repository at this point in the history
… ever

end up with uninitialized array elements
fortunately the compiler is able to eliminate the second conversion
making VARIABLES a cell array again. it's a hack to begin with...
  • Loading branch information
JeffBezanson committed Nov 2, 2011
1 parent a39ba7b commit fa87427
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 4 additions & 0 deletions j/array.j
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ function push{T}(a::Array{T,1}, item::ANY)
if is(T,None)
error("[] cannot grow. Instead, initialize the array with \"empty(element_type)\".")
end
# convert first so we don't grow the array if the assignment won't work
item = convert(T, item)
ccall(:jl_array_grow_end, Void, (Any, Ulong), a, ulong(1))
a[end] = item
return a
Expand All @@ -248,6 +250,7 @@ function enq{T}(a::Array{T,1}, item)
if is(T,None)
error("[] cannot grow. Instead, initialize the array with \"empty(element_type)\".")
end
item = convert(T, item)
ccall(:jl_array_grow_beg, Void, (Any, Ulong), a, ulong(1))
a[1] = item
return a
Expand All @@ -257,6 +260,7 @@ function insert{T}(a::Array{T,1}, i::Int, item)
if i < 1
throw(BoundsError())
end
item = convert(T, item)
l = length(a)
if i > l
ccall(:jl_array_grow_end, Void, (Any, Ulong), a, ulong(i-l))
Expand Down
2 changes: 1 addition & 1 deletion j/client.j
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function _start()
global const PGRP = ProcessGroup(0, {}, {})
end

global const VARIABLES = empty(Symbol)
global const VARIABLES = {}

# Load customized startup
try
Expand Down

0 comments on commit fa87427

Please sign in to comment.