forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jlapi.c
454 lines (403 loc) · 10.9 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// This file is a part of Julia. License is MIT: https://julialang.org/license
/*
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 "julia.h"
#include "options.h"
#include "julia_assert.h"
#include "julia_internal.h"
#ifdef __cplusplus
#include <cfenv>
extern "C" {
#else
#include <fenv.h>
#endif
#if defined(_OS_WINDOWS_) && !defined(_COMPILER_MINGW_)
JL_DLLEXPORT char * __cdecl dirname(char *);
#else
#include <libgen.h>
#endif
#ifndef _OS_WINDOWS_
#include <dlfcn.h>
#endif
JL_DLLEXPORT int jl_is_initialized(void)
{
return jl_main_module != NULL;
}
// First argument is the usr/bin directory where the julia binary is, or NULL to guess.
// 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
JL_DLLEXPORT void jl_init_with_image(const char *julia_bindir,
const char *image_relative_path)
{
if (jl_is_initialized())
return;
libsupport_init();
jl_options.julia_bindir = julia_bindir;
if (image_relative_path != NULL)
jl_options.image_file = image_relative_path;
else
jl_options.image_file = jl_get_default_sysimg_path();
julia_init(JL_IMAGE_JULIA_HOME);
jl_exception_clear();
}
JL_DLLEXPORT void jl_init(void)
{
char *libbindir = NULL;
#ifdef _OS_WINDOWS_
void *hdl = (void*)jl_load_dynamic_library(NULL, JL_RTLD_DEFAULT, 0);
if (hdl) {
char *to_free = (char*)jl_pathname_for_handle(hdl);
if (to_free) {
libbindir = strdup(dirname(to_free));
free(to_free);
}
}
#else
Dl_info dlinfo;
if (dladdr((void*)jl_init, &dlinfo) != 0 && dlinfo.dli_fname) {
char *to_free = strdup(dlinfo.dli_fname);
(void)asprintf(&libbindir, "%s" PATHSEPSTRING ".." PATHSEPSTRING "%s", dirname(to_free), "bin");
free(to_free);
}
#endif
if (!libbindir) {
printf("jl_init unable to find libjulia!\n");
abort();
}
jl_init_with_image(libbindir, jl_get_default_sysimg_path());
free(libbindir);
}
JL_DLLEXPORT jl_value_t *jl_eval_string(const char *str)
{
jl_value_t *r;
JL_TRY {
const char filename[] = "none";
jl_value_t *ast = jl_parse_all(str, strlen(str),
filename, strlen(filename));
JL_GC_PUSH1(&ast);
r = jl_toplevel_eval_in(jl_main_module, ast);
JL_GC_POP();
jl_exception_clear();
}
JL_CATCH {
jl_get_ptls_states()->previous_exception = jl_current_exception();
r = NULL;
}
return r;
}
JL_DLLEXPORT jl_value_t *jl_current_exception(void) JL_GLOBALLY_ROOTED
{
jl_excstack_t *s = jl_get_ptls_states()->current_task->excstack;
return s && s->top != 0 ? jl_excstack_exception(s, s->top) : jl_nothing;
}
JL_DLLEXPORT jl_value_t *jl_exception_occurred(void)
{
return jl_get_ptls_states()->previous_exception;
}
JL_DLLEXPORT void jl_exception_clear(void)
{
jl_get_ptls_states()->previous_exception = NULL;
}
// get the name of a type as a string
JL_DLLEXPORT const char *jl_typename_str(jl_value_t *v)
{
if (!jl_is_datatype(v))
return NULL;
return jl_symbol_name(((jl_datatype_t*)v)->name->name);
}
// get the name of typeof(v) as a string
JL_DLLEXPORT const char *jl_typeof_str(jl_value_t *v)
{
return jl_typename_str((jl_value_t*)jl_typeof(v));
}
JL_DLLEXPORT void *jl_array_eltype(jl_value_t *a)
{
return jl_tparam0(jl_typeof(a));
}
JL_DLLEXPORT int jl_array_rank(jl_value_t *a)
{
return jl_array_ndims(a);
}
JL_DLLEXPORT size_t jl_array_size(jl_value_t *a, int d)
{
return jl_array_dim(a, d);
}
JL_DLLEXPORT const char *jl_string_ptr(jl_value_t *s)
{
return jl_string_data(s);
}
JL_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];
size_t last_age = jl_get_ptls_states()->world_age;
jl_get_ptls_states()->world_age = jl_get_world_counter();
v = jl_apply(argv, nargs+1);
jl_get_ptls_states()->world_age = last_age;
JL_GC_POP();
jl_exception_clear();
}
JL_CATCH {
jl_get_ptls_states()->previous_exception = jl_current_exception();
v = NULL;
}
return v;
}
JL_DLLEXPORT jl_value_t *jl_call0(jl_function_t *f)
{
jl_value_t *v;
JL_TRY {
JL_GC_PUSH1(&f);
size_t last_age = jl_get_ptls_states()->world_age;
jl_get_ptls_states()->world_age = jl_get_world_counter();
v = jl_apply(&f, 1);
jl_get_ptls_states()->world_age = last_age;
JL_GC_POP();
jl_exception_clear();
}
JL_CATCH {
jl_get_ptls_states()->previous_exception = jl_current_exception();
v = NULL;
}
return v;
}
JL_DLLEXPORT jl_value_t *jl_call1(jl_function_t *f, jl_value_t *a)
{
jl_value_t *v;
JL_TRY {
jl_value_t **argv;
JL_GC_PUSHARGS(argv, 2);
argv[0] = f; argv[1] = a;
size_t last_age = jl_get_ptls_states()->world_age;
jl_get_ptls_states()->world_age = jl_get_world_counter();
v = jl_apply(argv, 2);
jl_get_ptls_states()->world_age = last_age;
JL_GC_POP();
jl_exception_clear();
}
JL_CATCH {
jl_get_ptls_states()->previous_exception = jl_current_exception();
v = NULL;
}
return v;
}
JL_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_value_t **argv;
JL_GC_PUSHARGS(argv, 3);
argv[0] = f; argv[1] = a; argv[2] = b;
size_t last_age = jl_get_ptls_states()->world_age;
jl_get_ptls_states()->world_age = jl_get_world_counter();
v = jl_apply(argv, 3);
jl_get_ptls_states()->world_age = last_age;
JL_GC_POP();
jl_exception_clear();
}
JL_CATCH {
jl_get_ptls_states()->previous_exception = jl_current_exception();
v = NULL;
}
return v;
}
JL_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_value_t **argv;
JL_GC_PUSHARGS(argv, 4);
argv[0] = f; argv[1] = a; argv[2] = b; argv[3] = c;
size_t last_age = jl_get_ptls_states()->world_age;
jl_get_ptls_states()->world_age = jl_get_world_counter();
v = jl_apply(argv, 4);
jl_get_ptls_states()->world_age = last_age;
JL_GC_POP();
jl_exception_clear();
}
JL_CATCH {
jl_get_ptls_states()->previous_exception = jl_current_exception();
v = NULL;
}
return v;
}
JL_DLLEXPORT void jl_yield(void)
{
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_call0(yieldfunc);
}
JL_DLLEXPORT jl_value_t *jl_get_field(jl_value_t *o, const 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 {
jl_get_ptls_states()->previous_exception = jl_current_exception();
v = NULL;
}
return v;
}
JL_DLLEXPORT void jl_sigatomic_begin(void)
{
JL_SIGATOMIC_BEGIN();
}
JL_DLLEXPORT void jl_sigatomic_end(void)
{
jl_ptls_t ptls = jl_get_ptls_states();
if (ptls->defer_signal == 0)
jl_error("sigatomic_end called in non-sigatomic region");
JL_SIGATOMIC_END();
}
JL_DLLEXPORT int jl_is_debugbuild(void)
{
#ifdef JL_DEBUG_BUILD
return 1;
#else
return 0;
#endif
}
JL_DLLEXPORT int8_t jl_is_memdebug(void) {
#ifdef MEMDEBUG
return 1;
#else
return 0;
#endif
}
JL_DLLEXPORT jl_value_t *jl_get_julia_bindir(void)
{
return jl_cstr_to_string(jl_options.julia_bindir);
}
JL_DLLEXPORT jl_value_t *jl_get_julia_bin(void)
{
return jl_cstr_to_string(jl_options.julia_bin);
}
JL_DLLEXPORT jl_value_t *jl_get_image_file(void)
{
return jl_cstr_to_string(jl_options.image_file);
}
JL_DLLEXPORT int jl_ver_major(void)
{
return JULIA_VERSION_MAJOR;
}
JL_DLLEXPORT int jl_ver_minor(void)
{
return JULIA_VERSION_MINOR;
}
JL_DLLEXPORT int jl_ver_patch(void)
{
return JULIA_VERSION_PATCH;
}
JL_DLLEXPORT int jl_ver_is_release(void)
{
return JULIA_VERSION_IS_RELEASE;
}
JL_DLLEXPORT const char *jl_ver_string(void)
{
return JULIA_VERSION_STRING;
}
// return char* from String field in Base.GIT_VERSION_INFO
static const char *git_info_string(const char *fld)
{
static jl_value_t *GIT_VERSION_INFO = NULL;
if (!GIT_VERSION_INFO)
GIT_VERSION_INFO = jl_get_global(jl_base_module, jl_symbol("GIT_VERSION_INFO"));
jl_value_t *f = jl_get_field(GIT_VERSION_INFO, fld);
assert(jl_is_string(f));
return jl_string_data(f);
}
JL_DLLEXPORT const char *jl_git_branch(void)
{
static const char *branch = NULL;
if (!branch) branch = git_info_string("branch");
return branch;
}
JL_DLLEXPORT const char *jl_git_commit(void)
{
static const char *commit = NULL;
if (!commit) commit = git_info_string("commit");
return commit;
}
// Create function versions of some useful macros
JL_DLLEXPORT jl_taggedvalue_t *(jl_astaggedvalue)(jl_value_t *v)
{
return jl_astaggedvalue(v);
}
JL_DLLEXPORT jl_value_t *(jl_valueof)(jl_taggedvalue_t *v)
{
return jl_valueof(v);
}
JL_DLLEXPORT jl_value_t *(jl_typeof)(jl_value_t *v)
{
return jl_typeof(v);
}
#ifndef __clang_analyzer__
JL_DLLEXPORT int8_t (jl_gc_unsafe_enter)(void)
{
jl_ptls_t ptls = jl_get_ptls_states();
return jl_gc_unsafe_enter(ptls);
}
JL_DLLEXPORT void (jl_gc_unsafe_leave)(int8_t state)
{
jl_ptls_t ptls = jl_get_ptls_states();
jl_gc_unsafe_leave(ptls, state);
}
JL_DLLEXPORT int8_t (jl_gc_safe_enter)(void)
{
jl_ptls_t ptls = jl_get_ptls_states();
return jl_gc_safe_enter(ptls);
}
JL_DLLEXPORT void (jl_gc_safe_leave)(int8_t state)
{
jl_ptls_t ptls = jl_get_ptls_states();
jl_gc_safe_leave(ptls, state);
}
#endif
JL_DLLEXPORT void (jl_gc_safepoint)(void)
{
jl_ptls_t ptls = jl_get_ptls_states();
jl_gc_safepoint_(ptls);
}
JL_DLLEXPORT void (jl_cpu_pause)(void)
{
jl_cpu_pause();
}
JL_DLLEXPORT void (jl_cpu_wake)(void)
{
jl_cpu_wake();
}
JL_DLLEXPORT void jl_get_fenv_consts(int *ret)
{
ret[0] = FE_INEXACT;
ret[1] = FE_UNDERFLOW;
ret[2] = FE_OVERFLOW;
ret[3] = FE_DIVBYZERO;
ret[4] = FE_INVALID;
ret[5] = FE_TONEAREST;
ret[6] = FE_UPWARD;
ret[7] = FE_DOWNWARD;
ret[8] = FE_TOWARDZERO;
}
#ifdef __cplusplus
}
#endif