Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement atomvm:random/0 on emscripten #694

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/platforms/emscripten/src/lib/platform_nifs.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <erl_nif_priv.h>
#include <globalcontext.h>
#include <interop.h>
#include <memory.h>
#include <nifs.h>
#include <term.h>
#include <term_typedef.h>
Expand All @@ -32,6 +33,8 @@
#include <emscripten/proxying.h>
#include <emscripten/threading.h>

#include <math.h>

//#define ENABLE_TRACE
#include <trace.h>

Expand All @@ -47,6 +50,20 @@ static term nif_atomvm_platform(Context *ctx, int argc, term argv[])
return EMSCRIPTEN_ATOM;
}

static term nif_atomvm_random(Context *ctx, int argc, term argv[])
{
UNUSED(ctx);
UNUSED(argc);
UNUSED(argv);
float val1 = emscripten_random();
float val2 = emscripten_random();
uint32_t result = ((uint32_t) floor(val1 * (1 << 16))) << 16 | ((uint32_t) floor(val2 * (1 << 16)));
if (UNLIKELY(memory_ensure_free(ctx, BOXED_INT64_SIZE) != MEMORY_GC_OK)) {
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
}
return term_make_maybe_boxed_int64(result, &ctx->heap);
}

static void do_run_script(GlobalContext *global, char *script, int sync, int sync_caller_pid)
{
emscripten_run_script(script);
Expand Down Expand Up @@ -143,6 +160,10 @@ static const struct Nif atomvm_platform_nif = {
.base.type = NIFFunctionType,
.nif_ptr = nif_atomvm_platform
};
static const struct Nif atomvm_random_nif = {
.base.type = NIFFunctionType,
.nif_ptr = nif_atomvm_random
};
static const struct Nif emscripten_run_script_nif = {
.base.type = NIFFunctionType,
.nif_ptr = nif_emscripten_run_script
Expand All @@ -162,6 +183,9 @@ const struct Nif *platform_nifs_get_nif(const char *nifname)
TRACE("Resolved platform nif %s ...\n", nifname);
return &atomvm_platform_nif;
}
if (strcmp("atomvm:random/0", nifname) == 0) {
return &atomvm_random_nif;
}
if (strcmp("emscripten:run_script/1", nifname) == 0) {
TRACE("Resolved platform nif %s ...\n", nifname);
return &emscripten_run_script_nif;
Expand Down
31 changes: 31 additions & 0 deletions src/platforms/emscripten/tests/cypress/e2e/atomvm.spec.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This file is part of AtomVM.
*
* Copyright 2023 by Paul Guyot <pguyot@kallisys.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
*/
describe("atomvm", () => {
beforeEach(() => {
cy.visit("/tests/src/test_atomvm.html");
});

it("should return platform", () => {
cy.get("#platform").should("contain", "emscripten");
});
it("should compute pi with a reasonable error", () => {
cy.get("#pierror").should("contain", "true");
});
});
2 changes: 2 additions & 0 deletions src/platforms/emscripten/tests/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ function(compile_erlang module_name)
)
endfunction()

compile_erlang(test_atomvm)
compile_erlang(test_call)

add_custom_target(emscripten_erlang_test_modules DEPENDS
test_atomvm.beam
test_call.beam
)
72 changes: 72 additions & 0 deletions src/platforms/emscripten/tests/src/test_atomvm.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
%
% This file is part of AtomVM.
%
% Copyright 2023 Paul Guyot <pguyot@kallisys.net>
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
%
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
%

-module(test_atomvm).
-export([start/0]).

start() ->
erlang:display({?MODULE, ?LINE}),
Platform = atomvm:platform(),
erlang:display({?MODULE, ?LINE}),
Random = atomvm:random(),
erlang:display({?MODULE, ?LINE}),
PI = pi_by_random(),
PIError = abs((PI - 3.141592653589793) / 3.141592653589793),
PIErrorThreshold = PIError < 0.01,
erlang:display({?MODULE, ?LINE}),
emscripten:run_script(
[
<<"document.querySelector('#platform').append('">>,
atom_to_list(Platform),
<<"');">>,
<<"document.querySelector('#random').append('">>,
integer_to_list(Random),
<<"');">>,
<<"document.querySelector('#pi').append('">>,
float_to_list(PI),
<<"');">>,
<<"document.querySelector('#pierror').append('">>,
atom_to_list(PIErrorThreshold),
<<"');">>
],
[main_thread, async]
),
loop().

loop() ->
receive
after infinity -> ok
end.

pi_by_random() ->
pi_by_random(100000, 0, 0).

pi_by_random(0, AccHit, AccTotal) ->
4 * AccHit / AccTotal;
pi_by_random(N, AccHit, AccTotal) ->
X = atomvm:random(),
Y = atomvm:random(),
SqDistance = (X - 16#80000000) * (X - 16#80000000) + (Y - 16#80000000) * (Y - 16#80000000),
NewAccHit =
case SqDistance > (16#80000000 * 16#80000000) of
true -> AccHit;
false -> AccHit + 1
end,
pi_by_random(N - 1, NewAccHit, AccTotal + 1).
44 changes: 44 additions & 0 deletions src/platforms/emscripten/tests/src/test_atomvm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!--
This file is part of AtomVM.

Copyright 2023 Paul Guyot <pguyot@kallisys.net>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
-->
<!doctype html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="platform"></div>
<div id="random"></div>
<div id="pi"></div>
<div id="pierror"></div>
<script>
// Arguments are loaded using fetch API.
// wasm_webserver serves under /tests/build/ files in src/platform/escripten/build/tests/src subdirectory
// and under /build/ files in build/ subdirectory.
var Module = {
arguments: [
"/tests/build/test_atomvm.beam",
"/build/libs/estdlib/src/estdlib.avm",
"/build/libs/eavmlib/src/eavmlib.avm",
],
};
</script>
<script async src="/AtomVM.js"></script>
</body>
</html>
Loading