Skip to content

Commit

Permalink
copy/peek for IOBuffer and some module introspection. Close #3229
Browse files Browse the repository at this point in the history
  • Loading branch information
Keno committed May 29, 2013
1 parent e4383fa commit 0fa66cb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ function push!(a::Array{Any,1}, item::ANY)
return a
end

function append!{T}(a::Array{T,1}, items::Array{T,1})
function append!{T}(a::Array{T,1}, items::Vector)
if is(T,None)
error("[] cannot grow. Instead, initialize the array with \"T[]\".")
end
Expand Down
16 changes: 16 additions & 0 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ type IOBuffer <: IO
new(data,readable,writable,seekable,append,length(data),maxsize,1)
end

function copy(b::IOBuffer)
ret = IOBuffer(b.writeable?copy(b.data):b.data,
b.readable,b.writable,b.seekable,b.append,b.maxsize)
ret.size = b.size
ret.ptr = b.ptr
ret
end

# PipeBuffers behave like Unix Pipes. They are readable and writable, the act appendable, and not seekable.
PipeBuffer(data::Vector{Uint8},maxsize::Int) = IOBuffer(data,true,true,false,true,maxsize)
PipeBuffer(data::Vector{Uint8}) = PipeBuffer(data,typemax(Int))
Expand Down Expand Up @@ -55,6 +63,14 @@ function read(from::IOBuffer, ::Type{Uint8})
return byte
end

function peek(from::IOBuffer)
if !from.readable error("read failed") end
if from.ptr > from.size
throw(EOFError())
end
return from.data[from.ptr]
end

read{T}(from::IOBuffer, ::Type{Ptr{T}}) = convert(Ptr{T}, read(from, Uint))

# TODO: IOBuffer is not iterable, so doesn't really have a length.
Expand Down
4 changes: 2 additions & 2 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module_name(m::Module) = ccall(:jl_module_name, Any, (Any,), m)::Symbol
module_parent(m::Module) = ccall(:jl_module_parent, Any, (Any,), m)::Module
current_module() = ccall(:jl_get_current_module, Any, ())::Module

names(m::Module, all::Bool) = ccall(:jl_module_names, Array{Symbol,1}, (Any,Int32), m, all)
names(m::Module) = names(m,false)
names(m::Module, all::Bool, imported::Bool) = ccall(:jl_module_names, Array{Symbol,1}, (Any,Int32,Int32), m, all, imported)
names(m::Module) = names(m,false,false)
names(t::DataType) = t.names

function names(v)
Expand Down
17 changes: 15 additions & 2 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,20 @@ DLLEXPORT void jl_set_current_module(jl_value_t *m)
jl_current_module = (jl_module_t*)m;
}

DLLEXPORT jl_value_t *jl_module_names(jl_module_t *m, int all)
DLLEXPORT jl_value_t *jl_module_usings(jl_module_t *m)
{
jl_array_t *a = jl_alloc_array_1d(jl_array_any_type, 0);
JL_GC_PUSH1(&a);
for(int i=(int)m->usings.len-1; i >= 0; --i) {
jl_array_grow_end(a, 1);
jl_module_t *imp = (jl_module_t*)m->usings.items[i];
jl_cellset(a,jl_array_dim0(a)-1, (jl_value_t*)imp);
}
JL_GC_POP();
return (jl_value_t*)a;
}

DLLEXPORT jl_value_t *jl_module_names(jl_module_t *m, int all, int imported)
{
jl_array_t *a = jl_alloc_array_1d(jl_array_symbol_type, 0);
JL_GC_PUSH1(&a);
Expand All @@ -385,7 +398,7 @@ DLLEXPORT jl_value_t *jl_module_names(jl_module_t *m, int all)
for(i=1; i < m->bindings.size; i+=2) {
if (table[i] != HT_NOTFOUND) {
jl_binding_t *b = (jl_binding_t*)table[i];
if (b->exportp || (b->owner == m && (all || m == jl_main_module))) {
if (b->exportp || ((imported || b->owner == m) && (all || m == jl_main_module))) {
jl_array_grow_end(a, 1);
//XXX: change to jl_arrayset if array storage allocation for Array{Symbols,1} changes:
jl_cellset(a, jl_array_dim0(a)-1, (jl_value_t*)b->name);
Expand Down

0 comments on commit 0fa66cb

Please sign in to comment.