Skip to content

Commit

Permalink
extra checks when calling copy for structs as arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
eriksvedang committed Jul 6, 2016
1 parent 4d42659 commit 0518ae9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
15 changes: 15 additions & 0 deletions lisp/structs.carp
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@

;; Should there be a way to mark existing C-structs?
;; (defmacro register-struct [struct-name]
;; (register-struct-internal (str struct-name)))

;; (defn register-struct-internal [struct-name]
;; (let [type-definition nil
;; group-name ""
;; dependency-level 0
;; copy-signature (list :fn (list (list :ref (keyword struct-name))) (keyword struct-name))]
;; (do
;; ;;(graph/add-node! :struct struct-name type-definition "" group-name nil '() dependency-level)
;; (graph/add-node! :struct (generic-name "copy" copy-signature) nil "" group-name nil '() dependency-level))))



(defmacro defstruct (struct-name struct-members)
(let [names-and-types (split-every-second (array-to-list struct-members))]
(list 'defstruct-internal
Expand Down
15 changes: 10 additions & 5 deletions src/call_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,23 @@ void call_foreign_function(Process *process, Obj *function, Obj **args, int arg_
bool argExpectsRef = p->car->tag == 'C' && obj_eq(process, p->car->car, type_ref);
//printf("argExpectsRef: %d\n", argExpectsRef);

goto noCopyOfArg;

if(args[i] == NULL || args[i]->meta == NULL) {
goto noCopyOfArg;
}
if(!argExpectsRef) {
Obj *type = env_lookup(process, args[i]->meta, obj_new_keyword("type"));
if(type) {
/* printf("Sending void_ptr as argument to ffi function, "); */
/* printf(" it's value is '%s' of type: ", STR(args[i])); */
/* printf("%s and tag %c, this should be copied! (for correctness)\n", STR(type), args[i]->tag); */

/* printf("Sending void_ptr as argument to ffi function %s, ", STR(function)); */
/* //printf(" it's value is '%s' ", STR(args[i])); */
/* printf("type %s and tag %c, this should be copied! (for correctness)\n", STR(type), args[i]->tag); */

Obj *copy = obj_copy(process, args[i]);

if(eval_error) {
return;
}

copy->meta = args[i]->meta;
shadow_stack_push(process, copy);
//printf("Copy with tag '%c': %s\n", copy->tag, STR(copy));
Expand Down
2 changes: 1 addition & 1 deletion src/obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ void bake_generic_primop_auto(Process *process, char *function_name, Obj *quoted
if(eval_error) {
printf("Error when calling bake-generic-primop-auto '%s' from C code: ", function_name);
printf("%s\n", obj_to_string(process, eval_error)->s);
function_trace_print(process);
//function_trace_print(process);
return;
}
else {
Expand Down

0 comments on commit 0518ae9

Please sign in to comment.