forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjlapi.c
278 lines (249 loc) · 6.3 KB
/
jlapi.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
jlapi.c
miscellaneous functions for users of libjulia.so, to handle initialization
and the style of use where julia is not in control most of the time.
*/
#include "platform.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "julia.h"
#ifdef __cplusplus
extern "C" {
#endif
#if defined(_OS_WINDOWS_) && !defined(_COMPILER_MINGW_)
DLLEXPORT char * __cdecl dirname(char *);
DLLEXPORT char * __cdecl basename(char *);
#else
#include <libgen.h>
#endif
DLLEXPORT char *jl_locate_sysimg(char *jlhome, char* imgpath)
{
if (jlhome == NULL) {
char *julia_path = (char*)malloc(512);
size_t path_size = 512;
uv_exepath(julia_path, &path_size);
julia_home = strdup(dirname(julia_path));
free(julia_path);
}
else {
julia_home = jlhome;
}
char path[512];
snprintf(path, sizeof(path), "%s%s%s",
julia_home, PATHSEPSTRING, imgpath);
return strdup(path);
}
DLLEXPORT void *jl_eval_string(char *str);
int jl_is_initialized(void) { return jl_main_module!=NULL; }
// First argument is the usr/lib directory where libjulia is, or NULL to guess.
// if that doesn't work, try the full path to the "lib" directory that
// contains lib/julia/sys.ji
// Second argument is the path of a system image file (*.ji) relative to the
// first argument path, or relative to the default julia home dir. The default
// is something like ../lib/julia/sys.ji
DLLEXPORT void jl_init_with_image(char *julia_home_dir, char *image_relative_path)
{
if (jl_is_initialized()) return;
libsupport_init();
if (image_relative_path == NULL)
image_relative_path = JL_SYSTEM_IMAGE_PATH;
char *image_file = jl_locate_sysimg(julia_home_dir, image_relative_path);
julia_init(image_file);
jl_set_const(jl_core_module, jl_symbol("JULIA_HOME"),
jl_cstr_to_string(julia_home));
jl_module_export(jl_core_module, jl_symbol("JULIA_HOME"));
jl_eval_string("Base.early_init()");
jl_eval_string("Base.init_head_sched()");
jl_eval_string("Base.init_load_path()");
jl_exception_clear();
}
DLLEXPORT void jl_init(char *julia_home_dir)
{
jl_init_with_image(julia_home_dir, JL_SYSTEM_IMAGE_PATH);
}
DLLEXPORT void *jl_eval_string(char *str)
{
#ifdef COPY_STACKS
int outside_task = (jl_root_task->stackbase == NULL);
if (outside_task) {
JL_SET_STACK_BASE;
}
#endif
jl_value_t *r;
JL_TRY {
jl_value_t *ast = jl_parse_input_line(str);
JL_GC_PUSH1(&ast);
r = jl_toplevel_eval(ast);
JL_GC_POP();
jl_exception_clear();
}
JL_CATCH {
//jl_show(jl_stderr_obj(), jl_exception_in_transit);
r = NULL;
}
#ifdef COPY_STACKS
if (outside_task) {
jl_root_task->stackbase = NULL;
}
#endif
return r;
}
DLLEXPORT jl_value_t *jl_exception_occurred(void)
{
return jl_is_null(jl_exception_in_transit) ? NULL :
jl_exception_in_transit;
}
DLLEXPORT void jl_exception_clear(void)
{
jl_exception_in_transit = (jl_value_t*)jl_null;
}
// get the name of a type as a string
DLLEXPORT const char *jl_typename_str(jl_value_t *v)
{
if (jl_is_tuple(v))
return "Tuple";
return ((jl_datatype_t*)v)->name->name->name;
}
// get the name of typeof(v) as a string
DLLEXPORT const char *jl_typeof_str(jl_value_t *v)
{
return jl_typename_str((jl_value_t*)jl_typeof(v));
}
DLLEXPORT void *jl_array_eltype(jl_value_t *a)
{
return jl_tparam0(jl_typeof(a));
}
DLLEXPORT int jl_array_rank(jl_value_t *a)
{
return jl_array_ndims(a);
}
DLLEXPORT size_t jl_array_size(jl_value_t *a, int d)
{
return jl_array_dim(a, d);
}
DLLEXPORT void *jl_array_ptr(jl_array_t *a);
DLLEXPORT const char *jl_bytestring_ptr(jl_value_t *s)
{
return jl_string_data(s);
}
DLLEXPORT jl_value_t *jl_call(jl_function_t *f, jl_value_t **args, int32_t nargs)
{
jl_value_t *v;
JL_TRY {
jl_value_t **argv;
JL_GC_PUSHARGS(argv, nargs+1);
argv[0] = (jl_value_t*) f;
for(int i=1; i<nargs+1; i++)
argv[i] = args[i-1];
v = jl_apply(f, args, nargs);
JL_GC_POP();
jl_exception_clear();
}
JL_CATCH {
v = NULL;
}
return v;
}
DLLEXPORT jl_value_t *jl_call0(jl_function_t *f)
{
jl_value_t *v;
JL_TRY {
JL_GC_PUSH1(&f);
v = jl_apply(f, NULL, 0);
JL_GC_POP();
jl_exception_clear();
}
JL_CATCH {
v = NULL;
}
return v;
}
DLLEXPORT jl_value_t *jl_call1(jl_function_t *f, jl_value_t *a)
{
jl_value_t *v;
JL_TRY {
JL_GC_PUSH2(&f,&a);
v = jl_apply(f, &a, 1);
JL_GC_POP();
jl_exception_clear();
}
JL_CATCH {
v = NULL;
}
return v;
}
DLLEXPORT jl_value_t *jl_call2(jl_function_t *f, jl_value_t *a, jl_value_t *b)
{
jl_value_t *v;
JL_TRY {
JL_GC_PUSH3(&f,&a,&b);
jl_value_t *args[2] = {a,b};
v = jl_apply(f, args, 2);
JL_GC_POP();
jl_exception_clear();
}
JL_CATCH {
v = NULL;
}
return v;
}
DLLEXPORT jl_value_t *jl_call3(jl_function_t *f, jl_value_t *a, jl_value_t *b, jl_value_t *c)
{
jl_value_t *v;
JL_TRY {
JL_GC_PUSH4(&f,&a,&b,&c);
jl_value_t *args[3] = {a,b,c};
v = jl_apply(f, args, 3);
JL_GC_POP();
jl_exception_clear();
}
JL_CATCH {
v = NULL;
}
return v;
}
DLLEXPORT void jl_yield()
{
static jl_function_t *yieldfunc = NULL;
if (yieldfunc == NULL)
yieldfunc = (jl_function_t*)jl_get_global(jl_base_module, jl_symbol("yield"));
if (yieldfunc != NULL && jl_is_func(yieldfunc))
jl_call0(yieldfunc);
}
DLLEXPORT jl_value_t *jl_get_field(jl_value_t *o, char *fld)
{
jl_value_t *v;
JL_TRY {
jl_value_t *s = (jl_value_t*)jl_symbol(fld);
int i = jl_field_index((jl_datatype_t*)jl_typeof(o), (jl_sym_t*)s, 1);
v = jl_get_nth_field(o, i);
jl_exception_clear();
}
JL_CATCH {
v = NULL;
}
return v;
}
DLLEXPORT void jl_sigatomic_begin(void)
{
JL_SIGATOMIC_BEGIN();
}
DLLEXPORT void jl_sigatomic_end(void)
{
if (jl_defer_signal == 0)
jl_error("sigatomic_end called in non-sigatomic region");
JL_SIGATOMIC_END();
}
DLLEXPORT int jl_is_debugbuild(void)
{
#ifdef DEBUG
return 1;
#else
return 0;
#endif
}
#ifdef __cplusplus
}
#endif