Skip to content

Commit

Permalink
Add additional bindings to 'tag' values and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peblair committed Oct 21, 2023
1 parent 053c185 commit 7afaa1b
Show file tree
Hide file tree
Showing 18 changed files with 592 additions and 94 deletions.
2 changes: 2 additions & 0 deletions src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
export
global
table
tag
memory
module_feature
module
Expand All @@ -37,6 +38,7 @@
export.js
global.js
table.js
tag.js
memory.js
module_feature.js
module.js
Expand Down
10 changes: 10 additions & 0 deletions src/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ caml_binaryen_add_global_export(value _module, value _internalName, value _exter
CAMLreturn(alloc_BinaryenExportRef(exp));
}

CAMLprim value
caml_binaryen_add_tag_export(value _module, value _internalName, value _externalName) {
CAMLparam3(_module, _internalName, _externalName);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
char* internalName = Safe_String_val(_internalName);
char* externalName = Safe_String_val(_externalName);
BinaryenExportRef exp = BinaryenAddTagExport(module, internalName, externalName);
CAMLreturn(alloc_BinaryenExportRef(exp));
}

CAMLprim value
caml_binaryen_get_export(value _module, value _externalName) {
CAMLparam2(_module, _externalName);
Expand Down
13 changes: 13 additions & 0 deletions src/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ function caml_binaryen_add_global_export(
);
}

//Provides: caml_binaryen_add_tag_export
//Requires: caml_jsstring_of_string
function caml_binaryen_add_tag_export(
wasm_mod,
internal_name,
external_name
) {
return wasm_mod.addTagExport(
caml_jsstring_of_string(internal_name),
caml_jsstring_of_string(external_name)
);
}

//Provides: caml_binaryen_get_export
//Requires: caml_jsstring_of_string
function caml_binaryen_get_export(wasm_mod, external_name) {
Expand Down
4 changes: 4 additions & 0 deletions src/export.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ external add_global_export : Module.t -> string -> string -> t
= "caml_binaryen_add_global_export"
(** Module, internal name, external name. *)

external add_tag_export : Module.t -> string -> string -> t
= "caml_binaryen_add_tag_export"
(** Module, internal name, external name. *)

external get_export : Module.t -> string -> t = "caml_binaryen_get_export"

external remove_export : Module.t -> string -> unit
Expand Down
1 change: 1 addition & 0 deletions src/export.mli
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ val add_function_export : Module.t -> string -> string -> t
val add_table_export : Module.t -> string -> string -> t
val add_memory_export : Module.t -> string -> string -> t
val add_global_export : Module.t -> string -> string -> t
val add_tag_export : Module.t -> string -> string -> t
val get_export : Module.t -> string -> t
val remove_export : Module.t -> string -> unit
val get_num_exports : Module.t -> int
Expand Down
4 changes: 2 additions & 2 deletions src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,7 @@ function caml_binaryen_ref_eq(wasm_mod, left, right) {
//Requires: caml_jsstring_of_string, caml_list_to_js_array
function caml_binaryen_try_native(wasm_mod, name, body, catch_tags, catch_bodies, delegate_target) {
return wasm_mod.try(
caml_jsstring_of_string(name),
name ? caml_jsstring_of_string(name[1]) : null,
body,
caml_list_to_js_array(catch_tags).map(caml_jsstring_of_string),
caml_list_to_js_array(catch_bodies),
Expand All @@ -1736,7 +1736,7 @@ function caml_binaryen_throw(wasm_mod, tag, operands) {
}

//Provides: caml_binaryen_rethrow
//Requires: caml_jsstring_of_string, caml_list_to_js_array
//Requires: caml_jsstring_of_string
function caml_binaryen_rethrow(wasm_mod, target) {
return wasm_mod.rethrow(
caml_jsstring_of_string(target),
Expand Down
32 changes: 32 additions & 0 deletions src/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ caml_binaryen_add_global_import__bytecode(value * argv) {
return caml_binaryen_add_global_import(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
}

CAMLprim value
caml_binaryen_add_tag_import(value _module, value _internalName, value _externalModuleName, value _externalBaseName, value _paramsty, value _resultsty) {
CAMLparam5(_module, _internalName, _externalModuleName, _externalBaseName, _paramsty);
CAMLxparam1(_resultsty);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
char* internalName = Safe_String_val(_internalName);
char* externalModuleName = Safe_String_val(_externalModuleName);
char* externalBaseName = Safe_String_val(_externalBaseName);
BinaryenType paramsty = BinaryenType_val(_paramsty);
BinaryenType resultsty = BinaryenType_val(_resultsty);
BinaryenAddTagImport(module, internalName, externalModuleName, externalBaseName, paramsty, resultsty);
CAMLreturn(Val_unit);
}
CAMLprim value
caml_binaryen_add_tag_import__bytecode(value * argv) {
return caml_binaryen_add_tag_import(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
}

CAMLprim value
caml_binaryen_function_import_get_module(value _fun) {
CAMLparam1(_fun);
Expand All @@ -89,6 +107,13 @@ caml_binaryen_global_import_get_module(value _global) {
CAMLreturn(caml_copy_string(BinaryenGlobalImportGetModule(global)));
}

CAMLprim value
caml_binaryen_tag_import_get_module(value _tag) {
CAMLparam1(_tag);
BinaryenTagRef tag = BinaryenTagRef_val(_tag);
CAMLreturn(caml_copy_string(BinaryenTagImportGetModule(tag)));
}

CAMLprim value
caml_binaryen_function_import_get_base(value _fun) {
CAMLparam1(_fun);
Expand All @@ -110,3 +135,10 @@ caml_binaryen_global_import_get_base(value _global) {
BinaryenGlobalRef global = BinaryenGlobalRef_val(_global);
CAMLreturn(caml_copy_string(BinaryenGlobalImportGetBase(global)));
}

CAMLprim value
caml_binaryen_tag_import_get_base(value _tag) {
CAMLparam1(_tag);
BinaryenTagRef tag = BinaryenTagRef_val(_tag);
CAMLreturn(caml_copy_string(BinaryenTagImportGetBase(tag)));
}
47 changes: 47 additions & 0 deletions src/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,37 @@ function caml_binaryen_add_global_import__bytecode() {
);
}

//Provides: caml_binaryen_add_tag_import
//Requires: caml_jsstring_of_string
function caml_binaryen_add_tag_import(
wasm_mod,
internalName,
externalModuleName,
externalBaseName,
paramsty,
resultsty
) {
return wasm_mod.addTagImport(
caml_jsstring_of_string(internalName),
caml_jsstring_of_string(externalModuleName),
caml_jsstring_of_string(externalBaseName),
paramsty,
resultsty
);
}
//Provides: caml_binaryen_add_tag_import__bytecode
//Requires: caml_binaryen_add_tag_import
function caml_binaryen_add_tag_import__bytecode() {
return caml_binaryen_add_tag_import(
arguments[0],
arguments[1],
arguments[2],
arguments[3],
arguments[4],
arguments[5]
);
}

//Provides: caml_binaryen_function_import_get_module
//Requires: Binaryen
//Requires: caml_string_of_jsstring
Expand All @@ -115,6 +146,14 @@ function caml_binaryen_global_import_get_module(global) {
return caml_string_of_jsstring(global_info.module);
}

//Provides: caml_binaryen_tag_import_get_module
//Requires: Binaryen
//Requires: caml_string_of_jsstring
function caml_binaryen_tag_import_get_module(tag) {
var tag_info = Binaryen.getTagInfo(tag);
return caml_string_of_jsstring(tag_info.module);
}

//Provides: caml_binaryen_function_import_get_base
//Requires: Binaryen
//Requires: caml_string_of_jsstring
Expand All @@ -137,3 +176,11 @@ function caml_binaryen_global_import_get_base(global) {
var global_info = Binaryen.getGlobalInfo(global);
return caml_string_of_jsstring(global_info.base);
}

//Provides: caml_binaryen_tag_import_get_base
//Requires: Binaryen
//Requires: caml_string_of_jsstring
function caml_binaryen_tag_import_get_base(tag) {
var tag_info = Binaryen.getTagInfo(tag);
return caml_string_of_jsstring(tag_info.base);
}
11 changes: 11 additions & 0 deletions src/import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ external add_global_import :
= "caml_binaryen_add_global_import__bytecode" "caml_binaryen_add_global_import"
(** Module, internal name, external module name, external base name, type, mutable. *)

external add_tag_import :
Module.t -> string -> string -> string -> Type.t -> Type.t -> unit
= "caml_binaryen_add_tag_import__bytecode" "caml_binaryen_add_tag_import"
(** Module, internal name, external module name, external base name, params type, results type. *)

external function_import_get_module : Function.t -> string
= "caml_binaryen_function_import_get_module"

Expand All @@ -26,6 +31,9 @@ external memory_import_get_module : Module.t -> string -> string
external global_import_get_module : Global.t -> string
= "caml_binaryen_global_import_get_module"

external tag_import_get_module : Tag.t -> string
= "caml_binaryen_tag_import_get_module"

external function_import_get_base : Function.t -> string
= "caml_binaryen_function_import_get_base"

Expand All @@ -34,3 +42,6 @@ external memory_import_get_base : Module.t -> string -> string

external global_import_get_base : Global.t -> string
= "caml_binaryen_global_import_get_base"

external tag_import_get_base : Tag.t -> string
= "caml_binaryen_tag_import_get_base"
5 changes: 5 additions & 0 deletions src/import.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ val add_memory_import : Module.t -> string -> string -> string -> bool -> unit
val add_global_import :
Module.t -> string -> string -> string -> Type.t -> bool -> unit

val add_tag_import :
Module.t -> string -> string -> string -> Type.t -> Type.t -> unit

val function_import_get_module : Function.t -> string
val memory_import_get_module : Module.t -> string -> string
val global_import_get_module : Global.t -> string
val tag_import_get_module : Tag.t -> string
val function_import_get_base : Function.t -> string
val memory_import_get_base : Module.t -> string -> string
val global_import_get_base : Global.t -> string
val tag_import_get_base : Tag.t -> string
8 changes: 8 additions & 0 deletions src/ocaml_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ value alloc_BinaryenGlobalRef(BinaryenGlobalRef exp)
return v;
}

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

/* Allocating an OCaml custom block to hold the given BinaryenExportRef */
value alloc_BinaryenExportRef(BinaryenExportRef exp)
{
Expand Down
4 changes: 4 additions & 0 deletions src/ocaml_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ static struct custom_operations binaryen_ops = {
#define BinaryenLiteral_val(v) (*((struct BinaryenLiteral*) Data_custom_val(v)))
#define BinaryenFunctionRef_val(v) (*((BinaryenFunctionRef*) Data_custom_val(v)))
#define BinaryenGlobalRef_val(v) (*((BinaryenGlobalRef*) Data_custom_val(v)))
#define BinaryenTagRef_val(v) (*((BinaryenTagRef*) Data_custom_val(v)))
#define BinaryenExportRef_val(v) (*((BinaryenExportRef*) Data_custom_val(v)))
#define BinaryenTableRef_val(v) (*((BinaryenTableRef*) Data_custom_val(v)))
#define BinaryenElementSegmentRef_val(v) (*((BinaryenElementSegmentRef*) Data_custom_val(v)))
Expand All @@ -53,6 +54,9 @@ value alloc_BinaryenType(BinaryenType typ);
/* Allocating an OCaml custom block to hold the given BinaryenGlobalRef */
value alloc_BinaryenGlobalRef(BinaryenGlobalRef exp);

/* Allocating an OCaml custom block to hold the given BinaryenTagRef */
value alloc_BinaryenTagRef(BinaryenTagRef exp);

/* Allocating an OCaml custom block to hold the given BinaryenExportRef */
value alloc_BinaryenExportRef(BinaryenExportRef exp);

Expand Down
61 changes: 61 additions & 0 deletions src/tag.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#define CAML_NAME_SPACE
#include <caml/mlvalues.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/alloc.h>

#include "binaryen-c.h"
#include "ocaml_helpers.h"

CAMLprim value
caml_binaryen_add_tag(value _module, value _name, value _params, value _results) {
CAMLparam4(_module, _name, _params, _results);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
char* name = Safe_String_val(_name);
BinaryenType params = BinaryenType_val(_params);
BinaryenType results = BinaryenType_val(_results);
BinaryenTagRef tag = BinaryenAddTag(module, name, params, results);
CAMLreturn(alloc_BinaryenTagRef(tag));
}

CAMLprim value
caml_binaryen_get_tag(value _module, value _name) {
CAMLparam2(_module, _name);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
char* name = Safe_String_val(_name);
BinaryenTagRef tag = BinaryenGetTag(module, name);
CAMLreturn(alloc_BinaryenTagRef(tag));
}

CAMLprim value
caml_binaryen_remove_tag(value _module, value _name) {
CAMLparam2(_module, _name);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
char* name = Safe_String_val(_name);
BinaryenRemoveTag(module, name);
CAMLreturn(Val_unit);
}

CAMLprim value
caml_binaryen_tag_get_name(value _tag) {
CAMLparam1(_tag);
BinaryenTagRef tag = BinaryenTagRef_val(_tag);
const char* name = BinaryenTagGetName(tag);
CAMLreturn(caml_copy_string(name));
}

CAMLprim value
caml_binaryen_tag_get_params(value _tag) {
CAMLparam1(_tag);
BinaryenTagRef tag = BinaryenTagRef_val(_tag);
BinaryenType ty = BinaryenTagGetParams(tag);
CAMLreturn(alloc_BinaryenType(ty));
}

CAMLprim value
caml_binaryen_tag_get_results(value _tag) {
CAMLparam1(_tag);
BinaryenTagRef tag = BinaryenTagRef_val(_tag);
BinaryenType ty = BinaryenTagGetResults(tag);
CAMLreturn(alloc_BinaryenType(ty));
}
43 changes: 43 additions & 0 deletions src/tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//Provides: caml_binaryen_add_tag
//Requires: caml_jsstring_of_string
function caml_binaryen_add_tag(wasm_mod, name, params, results) {
return wasm_mod.addTag(
caml_jsstring_of_string(name),
params,
results,
);
}

//Provides: caml_binaryen_get_tag
//Requires: caml_jsstring_of_string
function caml_binaryen_get_tag(wasm_mod, name) {
return wasm_mod.getTag(caml_jsstring_of_string(name));
}

//Provides: caml_binaryen_remove_tag
//Requires: caml_jsstring_of_string
function caml_binaryen_remove_tag(wasm_mod, name) {
return wasm_mod.removeTag(caml_jsstring_of_string(name));
}

//Provides: caml_binaryen_tag_get_name
//Requires: Binaryen
//Requires: caml_string_of_jsstring
function caml_binaryen_tag_get_name(tag) {
var tag_info = Binaryen.getTagInfo(tag);
return caml_string_of_jsstring(tag_info.name);
}

//Provides: caml_binaryen_tag_get_params
//Requires: Binaryen
function caml_binaryen_tag_get_params(tag) {
var tag_info = Binaryen.getTagInfo(tag);
return tag_info.params;
}

//Provides: caml_binaryen_tag_get_results
//Requires: Binaryen
function caml_binaryen_tag_get_results(tag) {
var tag_info = Binaryen.getTagInfo(tag);
return tag_info.results;
}
Loading

0 comments on commit 7afaa1b

Please sign in to comment.