Skip to content

Commit

Permalink
adding cfunction(func, RetType, (ArgTypes...)) callback interface
Browse files Browse the repository at this point in the history
addresses #1096
  • Loading branch information
JeffBezanson committed Nov 16, 2012
1 parent 93e8efc commit 24d2440
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base/base.jl
Expand Up @@ -121,6 +121,9 @@ dlsym(hnd, s::Symbol) = ccall(:jl_dlsym, Ptr{Void}, (Ptr{Void}, Ptr{Uint8}), hnd
dlsym_e(hnd, s::Symbol) = ccall(:jl_dlsym_e, Ptr{Void}, (Ptr{Void}, Ptr{Uint8}), hnd, s)
dlopen(s::String) = ccall(:jl_load_dynamic_library, Ptr{Void}, (Ptr{Uint8},), s)

cfunction(f::Function, r, a) =
ccall(:jl_function_ptr, Ptr{Void}, (Any, Any, Any), f, r, a)

identity(x) = x

function append_any(xs...)
Expand Down
1 change: 1 addition & 0 deletions base/export.jl
Expand Up @@ -1278,6 +1278,7 @@ export
pointer,
pointer_to_array,
ptr_arg_convert,
cfunction,
setenv,
strerror,
unsafe_ref,
Expand Down
27 changes: 27 additions & 0 deletions src/codegen.cpp
Expand Up @@ -270,6 +270,33 @@ const jl_value_t *jl_dump_function(jl_function_t *f, jl_tuple_t *types)
return jl_cstr_to_string((char*)stream.str().c_str());
}

static jl_value_t *ast_rettype(jl_value_t *ast);

extern "C" DLLEXPORT
void *jl_function_ptr(jl_function_t *f, jl_value_t *rt, jl_value_t *argt)
{
JL_TYPECHK(jl_function_ptr, type, rt);
JL_TYPECHK(jl_function_ptr, tuple, argt);
JL_TYPECHK(jl_function_ptr, type, argt);
if (jl_is_gf(f) && jl_is_leaf_type(rt) && jl_is_leaf_type(argt)) {
jl_function_t *ff = jl_get_specialization(f, (jl_tuple_t*)argt);
if (ff != NULL && ff->env == (jl_value_t*)jl_null && ff->linfo != NULL &&
ff->linfo->cFunctionObject != NULL) {
jl_lambda_info_t *li = ff->linfo;
if (jl_types_equal((jl_value_t*)li->specTypes, argt) &&
jl_types_equal(ast_rettype(li->ast), rt)) {
return jl_ExecutionEngine->getPointerToFunction((Function*)ff->linfo->cFunctionObject);
}
else {
jl_errorf("function_ptr: type signature of %s does not match",
li->name->name);
}
}
}
jl_error("function is not yet c-callable");
return NULL;
}

// information about the context of a piece of code: its enclosing
// function and module, and visible local variables and labels.
typedef struct {
Expand Down
1 change: 1 addition & 0 deletions src/julia.expmap
Expand Up @@ -75,6 +75,7 @@
jl_dlsym;
jl_dlsym_e;
jl_dump_function;
jl_function_ptr;
jl_enable_color;
jl_enable_inference;
jl_enq_send_req;
Expand Down

0 comments on commit 24d2440

Please sign in to comment.