forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.jl
178 lines (142 loc) · 4.34 KB
/
base.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# important core definitions
using Core.Intrinsics
import Core.Array # to add methods
convert(T, x) = convert_default(T, x, convert)
convert(T::Tuple, x::Tuple) = convert_tuple(T, x, convert)
abstract IO
type ErrorException <: Exception
msg::String
end
type SystemError <: Exception
prefix::String
errnum::Int32
SystemError(p::String, e::Integer) = new(p, int32(e))
SystemError(p::String) = new(p, errno())
end
type TypeError <: Exception
func::Symbol
context::String
expected::Type
got
end
type ParseError <: Exception
msg::String
end
type ArgumentError <: Exception
msg::String
end
#type UnboundError <: Exception
# var::Symbol
#end
type KeyError <: Exception
key
end
type LoadError <: Exception
file::String
line::Int
error
end
type MethodError <: Exception
f
args
end
type EOFError <: Exception end
type WeakRef
value
WeakRef() = WeakRef(nothing)
WeakRef(v::ANY) = ccall(:jl_gc_new_weakref, Any, (Any,), v)::WeakRef
end
ccall(:jl_get_system_hooks, Void, ())
int(x) = convert(Int, x)
int(x::Int) = x
uint(x) = convert(Uint, x)
uint(x::Uint) = x
# reflection
names(m::Module, all::Bool) = ccall(:jl_module_names, Array{Symbol,1}, (Any,Int32), m, all)
names(m::Module) = names(m,false)
module_name(m::Module) = ccall(:jl_module_name, Any, (Any,), m)::Symbol
module_parent(m::Module) = ccall(:jl_module_parent, Any, (Any,), m)::Module
names(t::DataType) = t.names
function names(v)
t = typeof(v)
if isa(t,DataType)
return names(t)
else
error("cannot call names() on a non-composite type")
end
end
# index colon
type Colon
end
const (:) = Colon()
hash(w::WeakRef) = hash(w.value)
isequal(w::WeakRef, v::WeakRef) = isequal(w.value, v.value)
isequal(w::WeakRef, v) = isequal(w.value, v)
isequal(w, v::WeakRef) = isequal(w, v.value)
function finalizer(o, f::Function)
if isimmutable(o)
error("objects of type ", typeof(o), " cannot be finalized")
end
ccall(:jl_gc_add_finalizer, Void, (Any,Any), o, f)
end
gc() = ccall(:jl_gc_collect, Void, ())
gc_enable() = ccall(:jl_gc_enable, Void, ())
gc_disable() = ccall(:jl_gc_disable, Void, ())
bytestring(str::ByteString) = str
# return an integer such that object_id(x)==object_id(y) if is(x,y)
object_id(x::ANY) = ccall(:jl_object_id, Uint, (Any,), x)
const isimmutable = x->(isa(x,Tuple) || !typeof(x).mutable)
isstructtype(t::DataType) = t.names!=() || (t.size==0 && !t.abstract)
isstructtype(x) = false
isbits(t::DataType) = !t.mutable && t.pointerfree
isbits(t::Tuple) = false
isbits(t::Type) = false
isbits(x) = isbits(typeof(x))
identity(x) = x
function append_any(xs...)
# used by apply() and quote
# must be a separate function from append(), since apply() needs this
# exact function.
out = Array(Any, 4)
l = 4
i = 1
for x in xs
for y in x
if i > l
ccall(:jl_array_grow_end, Void, (Any, Uint), out, 16)
l += 16
end
arrayset(out, y, i)
i += 1
end
end
ccall(:jl_array_del_end, Void, (Any, Uint), out, l-i+1)
out
end
macro thunk(ex); :(()->$(esc(ex))); end
macro L_str(s); s; end
function precompile(f, args::Tuple)
if isgeneric(f)
ccall(:jl_compile_hint, Void, (Any, Any), f, args)
end
end
# NOTE: Base shares Array with Core so we can add definitions to it
Array{T,N}(::Type{T}, d::NTuple{N,Int}) =
ccall(:jl_new_array, Array{T,N}, (Any,Any), Array{T,N}, d)
Array{N}(T, d::NTuple{N,Int}) =
(AT = Array{T,N};
ccall(:jl_new_array, Any, (Any,Any), AT, d)::AT)
Array{T}(::Type{T}, m::Int) =
ccall(:jl_alloc_array_1d, Array{T,1}, (Any,Int), Array{T,1}, m)
Array{T}(::Type{T}, m::Int,n::Int) =
ccall(:jl_alloc_array_2d, Array{T,2}, (Any,Int,Int), Array{T,2}, m,n)
Array{T}(::Type{T}, m::Int,n::Int,o::Int) =
ccall(:jl_alloc_array_3d, Array{T,3}, (Any,Int,Int,Int), Array{T,3}, m,n,o)
Array(T, d::Int...) = Array(T, d)
Array(T, d::Integer...) = Array(T, convert((Int...), d))
Array{T}(::Type{T}, m::Integer) =
ccall(:jl_alloc_array_1d, Array{T,1}, (Any,Int), Array{T,1}, m)
Array{T}(::Type{T}, m::Integer,n::Integer) =
ccall(:jl_alloc_array_2d, Array{T,2}, (Any,Int,Int), Array{T,2}, m, n)
Array{T}(::Type{T}, m::Integer,n::Integer,o::Integer) =
ccall(:jl_alloc_array_3d, Array{T,3}, (Any,Int,Int,Int), Array{T,3}, m, n, o)