fix incorrect folder name for julia-0.6.x
Former-commit-id: ef2c7401e0876f22d2f7762d182cfbcd5a7d9c70
This commit is contained in:
2
julia-0.6.3/share/doc/julia/examples/embedding/.gitignore
vendored
Normal file
2
julia-0.6.3/share/doc/julia/examples/embedding/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/embedding
|
||||
/embedding-debug
|
||||
53
julia-0.6.3/share/doc/julia/examples/embedding/Makefile
Normal file
53
julia-0.6.3/share/doc/julia/examples/embedding/Makefile
Normal file
@@ -0,0 +1,53 @@
|
||||
# This file is a part of Julia. License is MIT: https://julialang.org/license
|
||||
|
||||
# This Makefile template requires the following variables to be set
|
||||
# in the environment or on the command-line:
|
||||
# JULIA: path to julia[.exe] executable
|
||||
# BIN: binary build directory
|
||||
|
||||
ifndef JULIA
|
||||
$(error "Please pass JULIA=[path of target julia binary], or set as environment variable!")
|
||||
endif
|
||||
ifndef BIN
|
||||
$(error "Please pass BIN=[path of build directory], or set as environment variable!")
|
||||
endif
|
||||
|
||||
#=============================================================================
|
||||
# this source directory where embedding.c is located
|
||||
SRCDIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
|
||||
|
||||
# get the executable suffix, if any
|
||||
EXE := $(suffix $(abspath $(JULIA)))
|
||||
|
||||
# get compiler and linker flags. (see: `contrib/julia-config.jl`)
|
||||
JULIA_CONFIG := $(JULIA) -e 'include(joinpath(JULIA_HOME, Base.DATAROOTDIR, "julia", "julia-config.jl"))' --
|
||||
CPPFLAGS_ADD :=
|
||||
CFLAGS_ADD = $(shell $(JULIA_CONFIG) --cflags)
|
||||
LDFLAGS_ADD = -lm $(shell $(JULIA_CONFIG) --ldflags --ldlibs)
|
||||
|
||||
DEBUGFLAGS += -g
|
||||
|
||||
#=============================================================================
|
||||
|
||||
release: $(BIN)/embedding$(EXE)
|
||||
debug: $(BIN)/embedding-debug$(EXE)
|
||||
|
||||
$(BIN)/embedding$(EXE): $(SRCDIR)/embedding.c
|
||||
$(CC) $^ -o $@ $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS)
|
||||
|
||||
$(BIN)/embedding-debug$(EXE): $(SRCDIR)/embedding.c
|
||||
$(CC) $^ -o $@ $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) $(LDFLAGS_ADD) $(LDFLAGS) $(DEBUGFLAGS)
|
||||
|
||||
check: $(BIN)/embedding$(EXE)
|
||||
$(JULIA) $(SRCDIR)/embedding-test.jl $<
|
||||
@echo SUCCESS
|
||||
|
||||
clean:
|
||||
-rm -f $(BIN)/embedding-debug$(EXE) $(BIN)/embedding$(EXE)
|
||||
|
||||
.PHONY: release debug clean check
|
||||
|
||||
# Makefile debugging trick:
|
||||
# call print-VARIABLE to see the runtime value of any variable
|
||||
print-%:
|
||||
@echo '$*=$($*)'
|
||||
@@ -0,0 +1,22 @@
|
||||
# This file is a part of Julia. License is MIT: https://julialang.org/license
|
||||
|
||||
# tests the output of the embedding example is correct
|
||||
using Base.Test
|
||||
|
||||
@test length(ARGS) == 1
|
||||
@testset "embedding example" begin
|
||||
stdout = Pipe()
|
||||
stderr = Pipe()
|
||||
p = spawn(pipeline(Cmd(ARGS), stdin=DevNull, stdout=stdout, stderr=stderr))
|
||||
close(stdout.in)
|
||||
close(stderr.in)
|
||||
stdout_task = @async readlines(stdout)
|
||||
stderr = readstring(stderr)
|
||||
@test stderr == "MethodError: no method matching this_function_has_no_methods()\n"
|
||||
@test success(p)
|
||||
lines = wait(stdout_task)
|
||||
@test length(lines) == 9
|
||||
@test parse(Float64, lines[1]) ≈ sqrt(2)
|
||||
@test lines[8] == "called bar"
|
||||
@test lines[9] == "calling new bar"
|
||||
end
|
||||
153
julia-0.6.3/share/doc/julia/examples/embedding/embedding.c
Normal file
153
julia-0.6.3/share/doc/julia/examples/embedding/embedding.c
Normal file
@@ -0,0 +1,153 @@
|
||||
// This file is a part of Julia. License is MIT: https://julialang.org/license
|
||||
|
||||
#include <julia.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#ifdef _OS_WINDOWS_
|
||||
__declspec(dllexport) __cdecl
|
||||
#endif
|
||||
double my_c_sqrt(double x)
|
||||
{
|
||||
return sqrt(x);
|
||||
}
|
||||
|
||||
jl_value_t *checked_eval_string(const char* code)
|
||||
{
|
||||
jl_value_t *result = jl_eval_string(code);
|
||||
if (jl_exception_occurred()) {
|
||||
// none of these allocate, so a gc-root (JL_GC_PUSH) is not necessary
|
||||
jl_call2(jl_get_function(jl_base_module, "showerror"),
|
||||
jl_stderr_obj(),
|
||||
jl_exception_occurred());
|
||||
jl_printf(jl_stderr_stream(), "\n");
|
||||
jl_atexit_hook(1);
|
||||
exit(1);
|
||||
}
|
||||
assert(result && "Missing return value but no exception occurred!");
|
||||
return result;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
jl_init();
|
||||
|
||||
{
|
||||
// Simple running of Julia code
|
||||
|
||||
checked_eval_string("println(sqrt(2.0))");
|
||||
}
|
||||
|
||||
{
|
||||
// Accessing the return value
|
||||
|
||||
jl_value_t *ret = checked_eval_string("sqrt(2.0)");
|
||||
double retDouble = jl_unbox_float64(ret);
|
||||
printf("sqrt(2.0) in C: %e\n", retDouble);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
{
|
||||
// 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);
|
||||
double retDouble = jl_unbox_float64(ret);
|
||||
printf("sqrt(2.0) in C: %e\n", retDouble);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
{
|
||||
// 1D arrays
|
||||
|
||||
jl_value_t* array_type = jl_apply_array_type((jl_value_t*)jl_float64_type, 1);
|
||||
jl_array_t* x = jl_alloc_array_1d(array_type, 10);
|
||||
// JL_GC_PUSH* is required here to ensure that `x` is not deleted before
|
||||
// (aka, is gc-rooted until) the program reaches the corresponding JL_GC_POP()
|
||||
JL_GC_PUSH1(&x);
|
||||
|
||||
double* xData = jl_array_data(x);
|
||||
|
||||
size_t i;
|
||||
for (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);
|
||||
|
||||
printf("x = [");
|
||||
for (i = 0; i < jl_array_len(x); i++)
|
||||
printf("%e ", xData[i]);
|
||||
printf("]\n");
|
||||
fflush(stdout);
|
||||
|
||||
JL_GC_POP();
|
||||
}
|
||||
|
||||
{
|
||||
// Defining a Julia function and calling it
|
||||
|
||||
checked_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));
|
||||
|
||||
printf("my_func(5.0) = %f\n", ret);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
{
|
||||
// Calling a C function from Julia (from C)
|
||||
|
||||
// in a shared library (exported, by name)
|
||||
checked_eval_string("println( ccall(:my_c_sqrt, Float64, (Float64,), 2.0) )");
|
||||
|
||||
// or via a pointer
|
||||
jl_value_t *call_by_ptr = checked_eval_string(
|
||||
"my_c_sqrt -> println( ccall(my_c_sqrt, Float64, (Float64,), 2.0) )");
|
||||
jl_call1(call_by_ptr, jl_box_voidpointer(my_c_sqrt));
|
||||
}
|
||||
|
||||
{
|
||||
// Handling exceptions gracefully
|
||||
|
||||
jl_value_t *f = checked_eval_string("function this_function_has_no_methods end");
|
||||
jl_call0(f);
|
||||
|
||||
if (jl_exception_occurred()) {
|
||||
jl_call2(jl_get_function(jl_base_module, "showerror"),
|
||||
jl_stderr_obj(),
|
||||
jl_exception_occurred());
|
||||
jl_printf(jl_stderr_stream(), "\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
// Creating and using a native C function handle
|
||||
// to a Julia function signature
|
||||
|
||||
checked_eval_string(
|
||||
"function bar()\n"
|
||||
" println(\"called bar\")\n"
|
||||
" random_return_value = 42\n"
|
||||
"end");
|
||||
checked_eval_string(
|
||||
"function bar_from_c()\n"
|
||||
" bar()\n"
|
||||
" nothing\n"
|
||||
"end");
|
||||
typedef void (*Func_VOID__VOID)(void);
|
||||
jl_value_t *pbar = jl_eval_string("cfunction(bar_from_c, Void, ())");
|
||||
Func_VOID__VOID bar = (Func_VOID__VOID)jl_unbox_voidpointer(pbar);
|
||||
bar();
|
||||
checked_eval_string("bar() = println(\"calling new bar\")");
|
||||
bar();
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
jl_atexit_hook(ret);
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user