Skip to content

Commit

Permalink
Runtime: move runtime primitive around
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Jan 18, 2022
1 parent ec2fe44 commit dedbeaf
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 276 deletions.
8 changes: 0 additions & 8 deletions lib/js_of_ocaml/js_of_ocaml_stubs.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include <stdlib.h>
#include <stdio.h>
void caml_int64_create_lo_mi_hi () {
fprintf(stderr, "Unimplemented Javascript primitive caml_int64_create_lo_mi_hi!\n");
exit(1);
}
void caml_js_error_of_exception () {
fprintf(stderr, "Unimplemented Javascript primitive caml_js_error_of_exception!\n");
exit(1);
Expand All @@ -24,10 +20,6 @@ void caml_js_on_ie () {
fprintf(stderr, "Unimplemented Javascript primitive caml_js_on_ie!\n");
exit(1);
}
void caml_js_var () {
fprintf(stderr, "Unimplemented Javascript primitive caml_js_var!\n");
exit(1);
}
void caml_xmlhttprequest_create () {
fprintf(stderr, "Unimplemented Javascript primitive caml_xmlhttprequest_create!\n");
exit(1);
Expand Down
5 changes: 1 addition & 4 deletions lib/js_of_ocaml/json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ class type json =

let json : json Js.t = Unsafe.global##._JSON

external create_int64_lo_mi_hi : int -> int -> int -> Int64.t
= "caml_int64_create_lo_mi_hi"

let input_reviver =
let reviver _this _key (value : Unsafe.any) : Obj.t =
if typeof value == string "string"
Expand All @@ -48,7 +45,7 @@ let input_reviver =
&& Unsafe.get value 0 == 255
then
Obj.repr
(create_int64_lo_mi_hi
(Jsoo_runtime.Int64.create_int64_lo_mi_hi
(Unsafe.get value 1)
(Unsafe.get value 2)
(Unsafe.get value 3))
Expand Down
8 changes: 8 additions & 0 deletions lib/runtime/js_of_ocaml_runtime_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ void caml_exn_with_js_backtrace () {
fprintf(stderr, "Unimplemented Javascript primitive caml_exn_with_js_backtrace!\n");
exit(1);
}
void caml_int64_create_lo_mi_hi () {
fprintf(stderr, "Unimplemented Javascript primitive caml_int64_create_lo_mi_hi!\n");
exit(1);
}
void caml_js_call () {
fprintf(stderr, "Unimplemented Javascript primitive caml_js_call!\n");
exit(1);
Expand Down Expand Up @@ -132,6 +136,10 @@ void caml_js_typeof () {
fprintf(stderr, "Unimplemented Javascript primitive caml_js_typeof!\n");
exit(1);
}
void caml_js_var () {
fprintf(stderr, "Unimplemented Javascript primitive caml_js_var!\n");
exit(1);
}
void caml_js_wrap_callback () {
fprintf(stderr, "Unimplemented Javascript primitive caml_js_wrap_callback!\n");
exit(1);
Expand Down
7 changes: 7 additions & 0 deletions lib/runtime/jsoo_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ module For_compatibility_only = struct
external caml_list_of_js_array : Js.t -> 'a list = "caml_list_of_js_array"

external caml_list_to_js_array : 'a list -> Js.t = "caml_list_to_js_array"

external variable : string -> 'a = "caml_js_var"
end

module Typed_array = struct
Expand Down Expand Up @@ -179,3 +181,8 @@ module Typed_array = struct

external of_uint8Array : uint8Array -> string = "caml_string_of_array"
end

module Int64 = struct
external create_int64_lo_mi_hi : int -> int -> int -> Int64.t
= "caml_int64_create_lo_mi_hi"
end
276 changes: 224 additions & 52 deletions runtime/jslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,50 +35,6 @@ function caml_js_instanceof(o,c) { return o instanceof c; }
//Provides: caml_js_typeof (const)
function caml_js_typeof(o) { return typeof o; }

//Provides: caml_js_on_ie const
function caml_js_on_ie () {
var ua =
globalThis.navigator?globalThis.navigator.userAgent:"";
return ua.indexOf("MSIE") != -1 && ua.indexOf("Opera") != 0;
}

//Provides: caml_js_html_escape const (const)
var caml_js_regexps = { amp:/&/g, lt:/</g, quot:/\"/g, all:/[&<\"]/ };
function caml_js_html_escape (s) {
if (!caml_js_regexps.all.test(s)) return s;
return s.replace(caml_js_regexps.amp, "&amp;")
.replace(caml_js_regexps.lt, "&lt;")
.replace(caml_js_regexps.quot, "&quot;");
}

//Provides: caml_js_html_entities
//Requires: caml_failwith
function caml_js_html_entities(s) {
var entity = /^&#?[0-9a-zA-Z]+;$/
if(s.match(entity))
{
var str, temp = document.createElement('p');
temp.innerHTML= s;
str= temp.textContent || temp.innerText;
temp=null;
return str;
}
else {
caml_failwith("Invalid entity " + s);
}
}

/////////// Debugging console
//Provides: caml_js_get_console const
function caml_js_get_console () {
var c = globalThis.console?globalThis.console:{};
var m = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
"trace", "group", "groupCollapsed", "groupEnd", "time", "timeEnd"];
function f () {}
for (var i = 0; i < m.length; i++) if (!c[m[i]]) c[m[i]]=f;
return c;
}

//Provides:caml_trampoline
function caml_trampoline(res) {
var c = 1;
Expand Down Expand Up @@ -127,14 +83,11 @@ function js_print_stderr(s) {
}
}


//Provides: caml_is_js
function caml_is_js() {
return 1;
}



//Provides: caml_wrap_exception const (const)
//Requires: caml_global_data,caml_string_of_jsstring,caml_named_value
//Requires: caml_return_exn_constant
Expand Down Expand Up @@ -168,14 +121,233 @@ function caml_exn_with_js_backtrace(exn, force) {
return exn;
}

//Provides: caml_js_error_of_exception
function caml_js_error_of_exception(exn) {
if(exn.js_error) { return exn.js_error; }
return null;
}

//Provides: caml_js_error_option_of_exception
function caml_js_error_option_of_exception(exn) {
if(exn.js_error) { return [0, exn.js_error]; }
return 0;
}



//Provides: caml_js_from_bool const (const)
function caml_js_from_bool(x) { return !!x; }
//Provides: caml_js_to_bool const (const)
function caml_js_to_bool(x) { return +x; }
//Provides: caml_js_from_float const (const)
function caml_js_from_float(x) { return x; }
//Provides: caml_js_to_float const (const)
function caml_js_to_float(x) { return x; }

//Provides: caml_js_from_array mutable (shallow)
function caml_js_from_array(a) {
return a.slice(1);
}
//Provides: caml_js_to_array mutable (shallow)
function caml_js_to_array(a) {
var len = a.length;
var b = new Array(len+1);
b[0] = 0;
for(var i=0;i<len;i++) b[i+1] = a[i];
return b;
}

//Provides: caml_list_of_js_array const (const)
function caml_list_of_js_array(a){
var l = 0;
for(var i=a.length - 1; i>=0; i--){
var e = a[i];
l = [0,e,l];
}
return l
}

//Provides: caml_list_to_js_array const (const)
function caml_list_to_js_array(l){
var a = [];
for(; l !== 0; l = l[2]) {
a.push(l[1]);
}
return a;
}

//Provides: caml_js_var mutable (const)
//Requires: js_print_stderr
//Requires: caml_jsstring_of_string
function caml_js_var(x) {
var x = caml_jsstring_of_string(x);
//Checks that x has the form ident[.ident]*
if(!x.match(/^[a-zA-Z_$][a-zA-Z_$0-9]*(\.[a-zA-Z_$][a-zA-Z_$0-9]*)*$/)){
js_print_stderr("caml_js_var: \"" + x + "\" is not a valid JavaScript variable. continuing ..");
//globalThis.console.error("Js.Unsafe.eval_string")
}
return eval(x);
}
//Provides: caml_js_call (const, mutable, shallow)
//Requires: caml_js_from_array
function caml_js_call(f, o, args) { return f.apply(o, caml_js_from_array(args)); }
//Provides: caml_js_fun_call (const, shallow)
//Requires: caml_js_from_array
function caml_js_fun_call(f, a) {
switch (a.length) {
case 1: return f();
case 2: return f (a[1]);
case 3: return f (a[1],a[2]);
case 4: return f (a[1],a[2],a[3]);
case 5: return f (a[1],a[2],a[3],a[4]);
case 6: return f (a[1],a[2],a[3],a[4],a[5]);
case 7: return f (a[1],a[2],a[3],a[4],a[5],a[6]);
case 8: return f (a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
}
return f.apply(null, caml_js_from_array(a));
}
//Provides: caml_js_meth_call (mutable, const, shallow)
//Requires: caml_jsstring_of_string
//Requires: caml_js_from_array
function caml_js_meth_call(o, f, args) {
return o[caml_jsstring_of_string(f)].apply(o, caml_js_from_array(args));
}
//Provides: caml_js_new (const, shallow)
//Requires: caml_js_from_array
function caml_js_new(c, a) {
switch (a.length) {
case 1: return new c;
case 2: return new c (a[1]);
case 3: return new c (a[1],a[2]);
case 4: return new c (a[1],a[2],a[3]);
case 5: return new c (a[1],a[2],a[3],a[4]);
case 6: return new c (a[1],a[2],a[3],a[4],a[5]);
case 7: return new c (a[1],a[2],a[3],a[4],a[5],a[6]);
case 8: return new c (a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
}
function F() { return c.apply(this, caml_js_from_array(a)); }
F.prototype = c.prototype;
return new F;
}
//Provides: caml_ojs_new_arr (const, shallow)
//Requires: caml_js_from_array
function caml_ojs_new_arr(c, a) {
switch (a.length) {
case 0: return new c;
case 1: return new c (a[0]);
case 2: return new c (a[0],a[1]);
case 3: return new c (a[0],a[1],a[2]);
case 4: return new c (a[0],a[1],a[2],a[3]);
case 5: return new c (a[0],a[1],a[2],a[3],a[4]);
case 6: return new c (a[0],a[1],a[2],a[3],a[4],a[5]);
case 7: return new c (a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
}
function F() { return c.apply(this, a); }
F.prototype = c.prototype;
return new F;
}
//Provides: caml_js_wrap_callback const (const)
//Requires: caml_call_gen
function caml_js_wrap_callback(f) {
return function () {
var len = arguments.length;
if(len > 0){
var args = new Array(len);
for (var i = 0; i < len; i++) args[i] = arguments[i];
return caml_call_gen(f, args);
} else {
return caml_call_gen(f, [undefined]);
}
}
}

//Provides: caml_js_wrap_callback_arguments
//Requires: caml_call_gen
function caml_js_wrap_callback_arguments(f) {
return function() {
var len = arguments.length;
var args = new Array(len);
for (var i = 0; i < len; i++) args[i] = arguments[i];
return caml_call_gen(f, [args]);
}
}
//Provides: caml_js_wrap_callback_strict const
//Requires: caml_call_gen
function caml_js_wrap_callback_strict(arity, f) {
return function () {
var n = arguments.length;
if(n == arity && f.length == arity) return f.apply(null, arguments);
var args = new Array(arity);
var len = Math.min(arguments.length, arity)
for (var i = 0; i < len; i++) args[i] = arguments[i];
return caml_call_gen(f, args);
};
}
//Provides: caml_js_wrap_meth_callback const (const)
//Requires: caml_call_gen
function caml_js_wrap_meth_callback(f) {
return function () {
var len = arguments.length;
var args = new Array(len + 1);
args[0] = this;
for (var i = 0; i < len; i++) args[i+1] = arguments[i];
return caml_call_gen(f,args);
}
}
//Provides: caml_js_wrap_meth_callback_arguments const (const)
//Requires: caml_call_gen
function caml_js_wrap_meth_callback_arguments(f) {
return function () {
var len = arguments.length;
var args = new Array(len);
for (var i = 0; i < len; i++) args[i] = arguments[i];
return caml_call_gen(f,[this,args]);
}
}
//Provides: caml_js_wrap_meth_callback_strict const
//Requires: caml_call_gen
function caml_js_wrap_meth_callback_strict(arity, f) {
return function () {
var args = new Array(arity + 1);
var len = Math.min(arguments.length, arity)
args[0] = this;
for (var i = 0; i < len; i++) args[i+1] = arguments[i];
return caml_call_gen(f, args);
};
}
//Provides: caml_js_wrap_meth_callback_unsafe const (const)
//Requires: caml_call_gen
function caml_js_wrap_meth_callback_unsafe(f) {
return function () {
var len = arguments.length;
var args = new Array(len + 1);
args[0] = this;
for (var i = 0; i < len; i++) args[i+1] = arguments[i];
return f.apply(null, args); }
}
//Provides: caml_js_equals mutable (const, const)
function caml_js_equals (x, y) { return +(x == y); }

//Provides: caml_js_eval_string (const)
//Requires: caml_jsstring_of_string
function caml_js_eval_string (s) {return eval(caml_jsstring_of_string(s));}

//Provides: caml_js_expr (const)
//Requires: js_print_stderr
//Requires: caml_jsstring_of_string
function caml_js_expr(s) {
js_print_stderr("caml_js_expr: fallback to runtime evaluation\n");
return eval(caml_jsstring_of_string(s));}

//Provides: caml_pure_js_expr const (const)
//Requires: js_print_stderr
//Requires: caml_jsstring_of_string
function caml_pure_js_expr (s){
js_print_stderr("caml_pure_js_expr: fallback to runtime evaluation\n");
return eval(caml_jsstring_of_string(s));}

//Provides: caml_js_object (object_literal)
//Requires: caml_jsstring_of_string
function caml_js_object (a) {
var o = {};
for (var i = 1; i < a.length; i++) {
var p = a[i];
o[caml_jsstring_of_string(p[1])] = p[2];
}
return o;
}
Loading

0 comments on commit dedbeaf

Please sign in to comment.