Skip to content

Commit

Permalink
start adding builtin sizeof (ref #5406)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Aug 29, 2014
1 parent a0459b1 commit beffe01
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export
apply, fieldtype, getfield, setfield!, yieldto, throw, tuple, is, ===, isdefined,
# arraylen, arrayref, arrayset, arraysize, tuplelen, tupleref, convert_default,
# kwcall,
# sizeof # not exported, to avoid conflicting with Base.sizeof
# type reflection
issubtype, typeof, isa,
# typeassert, apply_type,
Expand Down
1 change: 1 addition & 0 deletions src/builtin_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ JL_CALLABLE(jl_f_new_module);
JL_CALLABLE(jl_f_throw);
JL_CALLABLE(jl_f_is);
JL_CALLABLE(jl_f_typeof);
JL_CALLABLE(jl_f_sizeof);
JL_CALLABLE(jl_f_subtype);
JL_CALLABLE(jl_f_isa);
JL_CALLABLE(jl_f_typeassert);
Expand Down
35 changes: 35 additions & 0 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,40 @@ JL_CALLABLE(jl_f_typeof)
return jl_full_type(args[0]);
}

JL_CALLABLE(jl_f_sizeof)
{
JL_NARGS(sizeof, 1, 1);
jl_value_t *x = args[0];
if (jl_is_datatype(x)) {
jl_datatype_t *dx = (jl_datatype_t*)x;
if (dx->abstract)
jl_error("argument is an abstract type; size is indeterminate");
if (!jl_is_bitstype(x)) {
if (dx->name == jl_array_typename || dx == jl_symbol_type)
jl_error("type does not have a canonical binary representation");
if (jl_tuple_len(dx->types) > 0) {
// TODO: return a definite size as long as all field types have
// been fully instantiated.
if (!jl_is_leaf_type(x))
jl_error("argument is an abstract type; size is indeterminate");
}
}
return jl_box_long(jl_datatype_size(x));
}
if (jl_is_array(x)) {
return jl_box_long(jl_array_len(x) * ((jl_array_t*)x)->elsize);
}
if (jl_is_tuple(x)) {
jl_error("tuples do not yet have a canonical binary representation");
}
jl_datatype_t *dt = (jl_datatype_t*)jl_typeof(x);
assert(jl_is_datatype(dt));
assert(!dt->abstract);
if (dt == jl_symbol_type)
jl_error("type does not have a canonical binary representation");
return jl_box_long(jl_datatype_size(dt));
}

JL_CALLABLE(jl_f_subtype)
{
JL_NARGS(subtype, 2, 2);
Expand Down Expand Up @@ -996,6 +1030,7 @@ void jl_init_primitives(void)
{
add_builtin_func("is", jl_f_is);
add_builtin_func("typeof", jl_f_typeof);
add_builtin_func("sizeof", jl_f_sizeof);
add_builtin_func("issubtype", jl_f_subtype);
add_builtin_func("isa", jl_f_isa);
add_builtin_func("typeassert", jl_f_typeassert);
Expand Down
2 changes: 1 addition & 1 deletion src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ void jl_init_serializer(void)
jl_f_invoke, jl_apply_generic,
jl_unprotect_stack, jl_f_task,
jl_f_yieldto, jl_f_ctor_trampoline,
jl_f_new_module,
jl_f_new_module, jl_f_sizeof,
NULL };
i=2;
while (fptrs[i-2] != NULL) {
Expand Down

0 comments on commit beffe01

Please sign in to comment.