Skip to content

Commit

Permalink
fix code generation of objects that refer to a variable in scope (#1183)
Browse files Browse the repository at this point in the history
* fix code generation of objects that refer to a variable in scope

* update submodule

* chore: add changelog entry
  • Loading branch information
anmonteiro authored Oct 6, 2024
1 parent d136c1d commit ff79856
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Unreleased
([#1164](https://github.com/melange-re/melange/pull/1164))
- Fix code generation of custom `true` / `false` constructors
([#1175](https://github.com/melange-re/melange/pull/1175))
- Fix code generation of OCaml objects that refers to an init variable in scope
([#1183](https://github.com/melange-re/melange/pull/1183))

4.0.1 2024-06-07
---------------
Expand Down
20 changes: 10 additions & 10 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions test/blackbox-tests/objects-close-over-variable.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
Test classes that close over an init variable from the outside scope

$ . ./setup.sh
$ cat > dune-project <<EOF
> (lang dune 3.9)
> (using melange 0.1)
> EOF
$ cat > dune <<EOF
> (melange.emit
> (target out)
> (preprocess
> (pps melange.ppx)))
> EOF
$ cat > x.ml <<EOF
> let fresh () =
> let r = 2 in
> let dummy = object
> val mutable x = r
> method get_x = x
> method set_x y = x <- y
> end in
> dummy
> let () =
> let f = (fresh()) in
> let x = f#get_x in
> Js.log2 "x expect 2" x;
> f#set_x 42 ;
> let y = f#get_x in
> Js.log2 "x expect 42" y;
> EOF
$ dune build @melange
$ node _build/default/out/x.js
x expect 2 2
x expect 42 42
$ cat _build/default/out/x.js
// Generated by Melange
'use strict';
const Caml_oo_curry = require("melange.js/caml_oo_curry.js");
const CamlinternalOO = require("melange/camlinternalOO.js");
const Curry = require("melange.js/curry.js");
const shared = [
"set_x",
"get_x"
];
const object_tables = {
TAG: /* Cons */0,
key: undefined,
data: undefined,
next: undefined
};
function fresh(param) {
if (!object_tables.key) {
const $$class = CamlinternalOO.create_table(shared);
const ids = CamlinternalOO.new_methods_variables($$class, shared, ["x"]);
const set_x = ids[0];
const get_x = ids[1];
const x = ids[2];
CamlinternalOO.set_methods($$class, [
get_x,
(function (self$1) {
return self$1[x];
}),
set_x,
(function (self$1, y) {
self$1[x] = y;
})
]);
const env_init = function (env) {
const self = CamlinternalOO.create_object_opt(undefined, $$class);
self[x] = env[1];
return self;
};
CamlinternalOO.init_class($$class);
object_tables.key = env_init;
}
return Curry._1(object_tables.key, [
undefined,
2
]);
}
const f = fresh(undefined);
const x = Caml_oo_curry.js1(291546447, 1, f);
console.log("x expect 2", x);
Caml_oo_curry.js2(-97543333, 2, f, 42);
const y = Caml_oo_curry.js1(291546447, 3, f);
console.log("x expect 42", y);
exports.fresh = fresh;
/* f Not a pure module */
2 changes: 1 addition & 1 deletion vendor/melange-compiler-libs

0 comments on commit ff79856

Please sign in to comment.