Skip to content

Commit

Permalink
feat: Add operations on functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer committed Mar 20, 2021
1 parent 4977b16 commit 5031fb9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
14 changes: 14 additions & 0 deletions js/function.ml
Expand Up @@ -23,7 +23,21 @@ let set_debug_location func expr file line column =
let get_function wasm_mod name =
meth_call wasm_mod "getFunction" [| inject (string name) |]

let get_function_by_index wasm_mod index =
meth_call wasm_mod "getFunctionByIndex" [| inject index |]

let remove_function wasm_mod name =
meth_call wasm_mod "removeFunction" [| inject (string name) |]

let get_num_functions wasm_mod = meth_call wasm_mod "getNumFunctions" [||]

let get_name func =
to_string
(meth_call global##.binaryen "_BinaryenFunctionGetName" [| inject func |])

let get_body func =
meth_call global##.binaryen "_BinaryenFunctionGetBody" [| inject func |]

let set_body func body =
meth_call global##.binaryen "_BinaryenFunctionSetBody"
[| inject func; inject body |]
41 changes: 41 additions & 0 deletions src/binaryen_stubs_functions.c
Expand Up @@ -2,6 +2,7 @@
#include <caml/mlvalues.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/alloc.h>

#include "binaryen-c.h"
#include "ocaml_helpers.h"
Expand All @@ -15,6 +16,14 @@ static value alloc_BinaryenFunctionRef(BinaryenFunctionRef fun)
return v;
}

/* Allocating an OCaml custom block to hold the given BinaryenExpressionRef */
static value alloc_BinaryenExpressionRef(BinaryenExpressionRef exp)
{
value v = caml_alloc_custom(&binaryen_ops, sizeof(BinaryenExpressionRef), 0, 1);
BinaryenExpressionRef_val(v) = exp;
return v;
}

CAMLprim value
caml_binaryen_add_function(value _module, value _name, value _params, value _results, value _locals, value _body) {
CAMLparam5(_module, _name, _params, _results, _locals);
Expand Down Expand Up @@ -62,6 +71,15 @@ caml_binaryen_get_num_functions(value _module) {
CAMLreturn(Val_int(BinaryenGetNumFunctions(module)));
}

CAMLprim value
caml_binaryen_get_function_by_index(value _module, value _index) {
CAMLparam2(_module, _index);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
BinaryenIndex index = Int_val(_index);
BinaryenFunctionRef fun = BinaryenGetFunctionByIndex(module, index);
CAMLreturn(alloc_BinaryenFunctionRef(fun));
}

CAMLprim value
caml_binaryen_set_start(value _module, value _fun) {
CAMLparam2(_module, _fun);
Expand All @@ -82,3 +100,26 @@ caml_binaryen_function_set_debug_location(value _fun, value _exp, value _file, v
BinaryenFunctionSetDebugLocation(fun, exp, file, line, column);
CAMLreturn(Val_unit);
}

CAMLprim value
caml_binaryen_function_get_name(value _fun) {
CAMLparam1(_fun);
BinaryenFunctionRef fun = BinaryenFunctionRef_val(_fun);
CAMLreturn(caml_copy_string(BinaryenFunctionGetName(fun)));
}

CAMLprim value
caml_binaryen_function_get_body(value _fun) {
CAMLparam1(_fun);
BinaryenFunctionRef fun = BinaryenFunctionRef_val(_fun);
CAMLreturn(alloc_BinaryenExpressionRef(BinaryenFunctionGetBody(fun)));
}

CAMLprim value
caml_binaryen_function_set_body(value _fun, value _body) {
CAMLparam2(_fun, _body);
BinaryenFunctionRef fun = BinaryenFunctionRef_val(_fun);
BinaryenExpressionRef body = BinaryenExpressionRef_val(_body);
BinaryenFunctionSetBody(fun, body);
CAMLreturn(Val_unit);
}
10 changes: 10 additions & 0 deletions src/function.ml
Expand Up @@ -12,7 +12,17 @@ external set_debug_location : t -> Expression.t -> int -> int -> int -> unit

external get_function : Module.t -> string -> t = "caml_binaryen_get_function"

external get_function_by_index : Module.t -> int -> t
= "caml_binaryen_get_function_by_index"

external remove_function : Module.t -> string -> unit
= "caml_binaryen_remove_function"

external get_num_functions : Module.t -> int = "caml_binaryen_get_num_functions"

external get_name : t -> string = "caml_binaryen_function_get_name"

external get_body : t -> Expression.t = "caml_binaryen_function_get_body"

external set_body : t -> Expression.t -> unit
= "caml_binaryen_function_set_body"
8 changes: 8 additions & 0 deletions virtual/function.mli
Expand Up @@ -9,6 +9,14 @@ val set_debug_location : t -> Expression.t -> int -> int -> int -> unit

val get_function : Module.t -> string -> t

val get_function_by_index : Module.t -> int -> t

val remove_function : Module.t -> string -> unit

val get_num_functions : Module.t -> int

val get_name : t -> string

val get_body : t -> Expression.t

val set_body : t -> Expression.t -> unit

0 comments on commit 5031fb9

Please sign in to comment.