6 /* gets the address of an object representing a C pointer, with the
7 intention of storing the pointer across code which may potentially GC. */
8 char *factor_vm::pinned_alien_offset(cell obj)
10 switch(tagged<object>(obj).type())
14 alien *ptr = untag<alien>(obj);
15 if(to_boolean(ptr->expired))
16 general_error(ERROR_EXPIRED,obj,false_object);
17 if(to_boolean(ptr->base))
18 type_error(ALIEN_TYPE,obj);
20 return (char *)ptr->address;
25 type_error(ALIEN_TYPE,obj);
26 return NULL; /* can't happen */
31 cell factor_vm::allot_alien(cell delegate_, cell displacement)
36 data_root<object> delegate(delegate_,this);
37 data_root<alien> new_alien(allot<alien>(sizeof(alien)),this);
39 if(delegate.type_p(ALIEN_TYPE))
41 tagged<alien> delegate_alien = delegate.as<alien>();
42 displacement += delegate_alien->displacement;
43 new_alien->base = delegate_alien->base;
46 new_alien->base = delegate.value();
48 new_alien->displacement = displacement;
49 new_alien->expired = false_object;
50 new_alien->update_address();
52 return new_alien.value();
55 cell factor_vm::allot_alien(void *address)
57 return allot_alien(false_object,(cell)address);
60 /* make an alien pointing at an offset of another alien */
61 void factor_vm::primitive_displaced_alien()
63 cell alien = ctx->pop();
64 cell displacement = to_cell(ctx->pop());
66 switch(tagged<object>(alien).type())
71 ctx->push(allot_alien(alien,displacement));
74 type_error(ALIEN_TYPE,alien);
79 /* address of an object representing a C pointer. Explicitly throw an error
80 if the object is a byte array, as a sanity check. */
81 void factor_vm::primitive_alien_address()
83 ctx->push(from_unsigned_cell((cell)pinned_alien_offset(ctx->pop())));
86 /* pop ( alien n ) from datastack, return alien's address plus n */
87 void *factor_vm::alien_pointer()
89 fixnum offset = to_fixnum(ctx->pop());
90 return alien_offset(ctx->pop()) + offset;
93 /* define words to read/write values at an alien address */
94 #define DEFINE_ALIEN_ACCESSOR(name,type,from,to) \
95 VM_C_API void primitive_alien_##name(factor_vm *parent) \
97 parent->ctx->push(parent->from(*(type*)(parent->alien_pointer()))); \
99 VM_C_API void primitive_set_alien_##name(factor_vm *parent) \
101 type *ptr = (type *)parent->alien_pointer(); \
102 type value = (type)parent->to(parent->ctx->pop()); \
106 EACH_ALIEN_PRIMITIVE(DEFINE_ALIEN_ACCESSOR)
108 /* open a native library and push a handle */
109 void factor_vm::primitive_dlopen()
111 data_root<byte_array> path(ctx->pop(),this);
112 path.untag_check(this);
113 data_root<dll> library(allot<dll>(sizeof(dll)),this);
114 library->path = path.value();
115 ffi_dlopen(library.untagged());
116 ctx->push(library.value());
119 /* look up a symbol in a native library */
120 void factor_vm::primitive_dlsym()
122 data_root<object> library(ctx->pop(),this);
123 data_root<byte_array> name(ctx->pop(),this);
124 name.untag_check(this);
126 symbol_char *sym = name->data<symbol_char>();
128 if(to_boolean(library.value()))
130 dll *d = untag_check<dll>(library.value());
132 if(d->handle == NULL)
133 ctx->push(false_object);
135 ctx->push(allot_alien(ffi_dlsym(d,sym)));
138 ctx->push(allot_alien(ffi_dlsym(NULL,sym)));
141 /* look up a symbol in a native library */
142 void factor_vm::primitive_dlsym_raw()
144 data_root<object> library(ctx->pop(),this);
145 data_root<byte_array> name(ctx->pop(),this);
146 name.untag_check(this);
148 symbol_char *sym = name->data<symbol_char>();
150 if(to_boolean(library.value()))
152 dll *d = untag_check<dll>(library.value());
154 if(d->handle == NULL)
155 ctx->push(false_object);
157 ctx->push(allot_alien(ffi_dlsym_raw(d,sym)));
160 ctx->push(allot_alien(ffi_dlsym_raw(NULL,sym)));
164 /* close a native library handle */
165 void factor_vm::primitive_dlclose()
167 dll *d = untag_check<dll>(ctx->pop());
168 if(d->handle != NULL)
172 void factor_vm::primitive_dll_validp()
174 cell library = ctx->pop();
175 if(to_boolean(library))
176 ctx->push(tag_boolean(untag_check<dll>(library)->handle != NULL));
178 ctx->push(true_object);
181 /* gets the address of an object representing a C pointer */
182 char *factor_vm::alien_offset(cell obj)
184 switch(tagged<object>(obj).type())
186 case BYTE_ARRAY_TYPE:
187 return untag<byte_array>(obj)->data<char>();
189 return (char *)untag<alien>(obj)->address;
193 type_error(ALIEN_TYPE,obj);
194 return NULL; /* can't happen */