1 module juliad.shims;
2 
3 import std.stdio;
4 
5 import juliad.julia;
6 
7 // #define jl_array_len(a)   (((jl_array_t*)(a))->length)
8 size_t jl_array_len(jl_value_t* a) {
9 	return jl_array_len(cast(jl_array_t*)a);
10 }
11 
12 size_t jl_array_len(jl_array_t* a) {
13 	return a.length;
14 }
15 
16 // #define jl_array_data(a)  ((void*)((jl_array_t*)(a))->data)
17 void* jl_array_data(jl_value_t* a) {
18 	return jl_array_data(cast(jl_array_t*)a);
19 }
20 
21 void* jl_array_data(jl_array_t* a) {
22 	return cast(void*)a.data;
23 }
24 
25 // #define jl_array_dim(a,i) ((&((jl_array_t*)(a))->nrows)[i])
26 size_t jl_array_dim(jl_value_t* a, size_t dim) {
27 	jl_array_t* arr = cast(jl_array_t*)a;
28 	return jl_array_dim(arr, dim);
29 }
30 
31 size_t jl_array_dim(jl_array_t* a, size_t dim) {
32 	size_t* r = &(a.nrows);
33 	return r[dim];
34 }
35 
36 // #define jl_array_ndims(a) ((int32_t)(((jl_array_t*)a)->flags.ndims))
37 uint jl_array_ndims(jl_value_t* a) {
38 	return jl_array_ndims(cast(jl_array_t*)a);
39 }
40 
41 uint jl_array_ndims(jl_array_t* a) {
42 	auto t = (*a).flags.ndims;
43 	return cast(uint)t;
44 }
45 
46 
47 // #define jl_typeof(v)                                                    \
48 //     ((jl_value_t*)(jl_astaggedvalue(v)->header & ~(uintptr_t)15))
49 jl_value_t* jl_typeof(jl_value_t* t) {
50 	return cast(jl_value_t*)(jl_astaggedvalue(t).header & ~cast(uint)15);
51 }
52 
53 //#define jl_astaggedvalue(v)                                             \
54 //    ((jl_taggedvalue_t*)((char*)(v) - sizeof(jl_taggedvalue_t)))
55 
56 jl_taggedvalue_t* jl_astaggedvalue(jl_value_t *v) {
57 	return cast(jl_taggedvalue_t*)((cast(char*)v) - jl_taggedvalue_t.sizeof);
58 }
59 
60 // #define jl_tparam0(t)  jl_svecref(((jl_datatype_t*)(t))->parameters, 0)
61 jl_value_t* jl_tparam0(jl_array_t* t) {
62 	return jl_tparam0(cast(jl_value_t*)t);
63 }
64 
65 jl_value_t* jl_tparam0(jl_value_t* t) {
66 	return jl_svecref((cast(jl_datatype_t*)t).parameters, 0);
67 }
68 
69 bool jl_is_array_type(jl_value_t* t) {
70     return jl_is_datatype(t) &&
71             (cast(jl_datatype_t*)t).name == jl_array_typename;
72 }
73 
74 //#define jl_svec_len(t)              (((jl_svec_t*)(t))->length)
75 size_t jl_svec_len(jl_svec_t* t) {
76 	return t.length;
77 }
78 
79 //#define jl_svec_data(t) ((jl_value_t**)((char*)(t) + sizeof(jl_svec_t)))
80 jl_value_t** jl_svec_data(jl_svec_t* t) {
81 	return cast(jl_value_t**)((cast(char*)t) + jl_svec_t.sizeof);
82 }
83 
84 jl_value_t *jl_svecref(jl_svec_t* t, size_t i) {
85     //assert(jl_typeis(cast(jl_value_t*)t, jl_simplevector_type));
86     //assert(i < jl_svec_len(t));
87     return jl_svec_data(t)[i];
88 }
89 
90 // #define jl_is_datatype(v)    jl_typeis(v,jl_datatype_type)
91 bool jl_is_datatype(jl_value_t* t) {
92 	return jl_typeis(t, jl_datatype_type);
93 }
94 
95 
96 // #define jl_typeis(v,t) (jl_typeof(v)==(jl_value_t*)(t))
97 bool jl_typeis(jl_value_t* v, jl_datatype_t* t) {
98 	return jl_typeof(v) == cast(jl_value_t*)t;
99 }
100 
101 // #define jl_pgcstack (jl_get_ptls_states()->pgcstack)
102 //_jl_gcframe_t* jl_pgcstack() {
103 //	return jl_get_ptls_states().pgcstack;
104 //}
105 
106 //void JL_GC_PUSH1(jl_value_t* arg1) {
107 //	void*[] __gc_stkf = 
108 //	    [ cast(void*)3, cast(void*)jl_get_ptls_states().pgcstack
109 //	    , cast(void*)arg1 ];
110 //	jl_get_ptls_states().pgcstack = cast(jl_gcframe_t*)__gc_stkf;
111 //}
112 
113 /*extern(C) {
114 void jl_gc_push1(jl_value_t** arg);
115 void jl_gc_push2(jl_value_t** arg, jl_value_t** arg1);
116 void jl_gc_push3(jl_value_t** arg, jl_value_t** arg1, jl_value_t** arg2);
117 void jl_gc_push4(jl_value_t** arg, jl_value_t** arg1, jl_value_t** arg2, 
118 		jl_value_t** arg3);
119 void jl_gc_push5(jl_value_t** arg, jl_value_t** arg1, jl_value_t** arg2, 
120 		jl_value_t** arg3, jl_value_t** arg4);
121 void jl_gc_push6(jl_value_t** arg, jl_value_t** arg1, jl_value_t** arg2, 
122 		jl_value_t** arg3, jl_value_t** arg4, jl_value_t** arg5);
123 void jl_gc_pop();
124 }*/
125 void jl_gc_push1(jl_value_t** arg) {
126 	void*[] __gc_stkf = 
127 	    [ cast(void*)3, cast(void*)jl_get_ptls_states().pgcstack
128 	    , cast(void*)arg ];
129 	jl_get_ptls_states().pgcstack = cast(jl_gcframe_t*)__gc_stkf;
130 }
131 
132 void jl_gc_pop() {
133 	jl_get_ptls_states().pgcstack = jl_get_ptls_states().pgcstack.prev;
134 }
135 
136 string getErrorString() {
137 	import std.conv : to;
138 	import std.string : fromStringz;
139 	return to!string(jl_typeof_str(jl_exception_occurred()).fromStringz);
140 }