Skip to content

Commit

Permalink
formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jan 30, 2014
1 parent 2e07004 commit 1e5087b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
86 changes: 42 additions & 44 deletions examples/embedding.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,85 +12,83 @@ int main()
jl_init(NULL);

{
// Simple running Julia code
// Simple running Julia code

jl_eval_string("println(sqrt(2.0))");
jl_eval_string("println(sqrt(2.0))");
}

{
// Accessing the return value
// Accessing the return value

jl_value_t *ret = jl_eval_string("sqrt(2.0)");
jl_value_t *ret = jl_eval_string("sqrt(2.0)");

if(jl_is_float64(ret))
{
double retDouble = jl_unbox_float64(ret);
printf("sqrt(2.0) in C: %e\n", retDouble);
}
if (jl_is_float64(ret)) {
double retDouble = jl_unbox_float64(ret);
printf("sqrt(2.0) in C: %e\n", retDouble);
}
}

{
// Same as above but with function handle (more flexible)
// Same as above but with function handle (more flexible)

jl_function_t *func = jl_get_function(jl_base_module, "sqrt");
jl_value_t* argument = jl_box_float64(2.0);
jl_value_t* ret = jl_call1(func, argument);
jl_function_t *func = jl_get_function(jl_base_module, "sqrt");
jl_value_t* argument = jl_box_float64(2.0);
jl_value_t* ret = jl_call1(func, argument);

if(jl_is_float64(ret))
{
double retDouble = jl_unbox_float64(ret);
printf("sqrt(2.0) in C: %e\n", retDouble);
}
if (jl_is_float64(ret)) {
double retDouble = jl_unbox_float64(ret);
printf("sqrt(2.0) in C: %e\n", retDouble);
}
}

{
// 1D arrays
// 1D arrays

jl_value_t* array_type = jl_apply_array_type( jl_float64_type, 1 );
jl_array_t* x = jl_alloc_array_1d(array_type , 10);
JL_GC_PUSH1(&x);
jl_value_t* array_type = jl_apply_array_type( jl_float64_type, 1 );
jl_array_t* x = jl_alloc_array_1d(array_type , 10);
JL_GC_PUSH1(&x);

double* xData = jl_array_data(x);
double* xData = jl_array_data(x);

for(size_t i=0; i<jl_array_len(x); i++)
xData[i] = i;
for(size_t i=0; i<jl_array_len(x); i++)
xData[i] = i;

jl_function_t *func = jl_get_function(jl_base_module, "reverse!");
jl_call1(func, (jl_value_t*) x);
jl_function_t *func = jl_get_function(jl_base_module, "reverse!");
jl_call1(func, (jl_value_t*) x);

printf("x = [");
for(size_t i=0; i<jl_array_len(x); i++)
printf("%e ", xData[i]);
printf("]\n");
printf("x = [");
for(size_t i=0; i<jl_array_len(x); i++)
printf("%e ", xData[i]);
printf("]\n");

JL_GC_POP();
JL_GC_POP();
}

{
// define julia function and call it
// define julia function and call it

jl_eval_string("my_func(x) = 2*x");
jl_eval_string("my_func(x) = 2*x");

jl_function_t *func = jl_get_function(jl_current_module, "my_func");
jl_value_t* arg = jl_box_float64(5.0);
double ret = jl_unbox_float64(jl_call1(func, arg));
jl_function_t *func = jl_get_function(jl_current_module, "my_func");
jl_value_t* arg = jl_box_float64(5.0);
double ret = jl_unbox_float64(jl_call1(func, arg));

printf("my_func(5.0) = %f\n", ret);
printf("my_func(5.0) = %f\n", ret);
}

{
// call c function
// call c function

jl_eval_string("println( ccall( :my_c_sqrt, Float64, (Float64,), 2.0 ) )");
jl_eval_string("println( ccall( :my_c_sqrt, Float64, (Float64,), 2.0 ) )");
}

{
// check for exceptions
// check for exceptions

jl_eval_string("this_function_does_not_exist()");
jl_eval_string("this_function_does_not_exist()");

if (jl_exception_occurred())
printf("%s \n", jl_get_exception_str( jl_exception_occurred() ) );
if (jl_exception_occurred())
printf("%s \n", jl_get_exception_str( jl_exception_occurred() ) );
}


Expand Down
6 changes: 3 additions & 3 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ DLLEXPORT jl_value_t *jl_eqtable_get(jl_array_t *h, void *key, jl_value_t *deflt
// system information
DLLEXPORT int jl_errno(void);
DLLEXPORT void jl_set_errno(int e);
DLLEXPORT int32_t jl_stat(const char* path, char* statbuf);
DLLEXPORT int32_t jl_stat(const char *path, char *statbuf);

// environment entries
DLLEXPORT jl_value_t *jl_environ(int i);
Expand Down Expand Up @@ -894,9 +894,9 @@ jl_value_t *jl_no_method_error(jl_function_t *f, jl_value_t **args, size_t na);
void jl_check_type_tuple(jl_tuple_t *t, jl_sym_t *name, const char *ctx);
DLLEXPORT jl_value_t *jl_exception_occurred(void);
DLLEXPORT void jl_exception_clear(void);
STATIC_INLINE char* jl_get_exception_str(jl_value_t* exception)
STATIC_INLINE char *jl_get_exception_str(jl_value_t *exception)
{
return jl_string_data( jl_fieldref( exception ,0 ) );
return jl_string_data(jl_fieldref(exception, 0));
}


Expand Down

0 comments on commit 1e5087b

Please sign in to comment.