Git Product home page Git Product logo

webassembly / js-bigint-integration Goto Github PK

View Code? Open in Web Editor NEW
36.0 41.0 14.0 12.4 MB

JavaScript BigInt to WebAssembly i64 integration

Home Page: https://webassembly.github.io/JS-BigInt-integration/js-api/index.html

License: Other

Makefile 0.60% Python 1.76% Batchfile 0.67% CSS 0.03% Shell 0.40% HTML 0.10% OCaml 7.19% Standard ML 0.05% WebAssembly 83.33% JavaScript 5.39% Perl 0.48%
proposal

js-bigint-integration's Introduction

WebAssembly BigInt<->i64 conversion in JS API

This proposal has been merged into the main specification.

With the changes in this repository, BigInts have support for bidirectionally converting to 64-bit integer WebAssembly values, which appears as

  • Parameters and return values to exported WebAssembly functions
  • Parameters and return values to host functions
  • Imported and exported globals

BigInts can be read or written from WebAssembly memory using the BigInt proposal's BigInt64Array or BigUint64Array; no additional support is needed.

Design issue

Rendered spec

js-bigint-integration's People

Contributors

andrewscheidecker avatar backes avatar binji avatar bnjbvr avatar cellule avatar chicoxyzzy avatar dschuff avatar flagxor avatar gahaas avatar ggreif avatar honry avatar hugoguiroux avatar jfbastien avatar juniorrojas avatar kg avatar kripken avatar lgalfaso avatar lindig avatar littledan avatar lukewagner avatar ms2ger avatar naturaltransformation avatar ngzhian avatar pepyakin avatar pjuftring avatar ppopth avatar rossberg avatar sunfishcode avatar swasey avatar xtuc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-bigint-integration's Issues

Allow passing non-bigint values to I64 parameters

At the moment, ToWebAssemblyValue(value) calls ToBigInt64(value) if the expected type is I64. ToBigInt64 throws a TypeError if value is a Number.

This behavior looks unexpected and inconsistent to me, because for all other types, ToWebAssemblyValue works differently.

I'm not convinced that we have to couple wasm.I64 so tightly to js.BigInt. BigInt is needed to allow wasm.I64 to be passed precisely to JavaScript, and also to big numbers precisely from JavaScript to WebAssembly. However, this does not mean that it should not be possible to pass normal numbers to WebAssembly as I64 parameters.

One scenario that comes to my mind is the following: if I want to pass an object to WebAssembly as an I32 parameter, I just have to add the valueOf function to the object to return a number. For an I64 parameter, however, I would have to add a toString function and avoid the valueOf function to avoid a TypeError. [edit] The valueOf function could also return a BigInt, but then it cannot be used for I32 parameters anymore.

Add test for {valueOf() {...}}

Port from v8:

(function TestI64GlobalValueOf() {
  var argument = { "value": "i64" };
  var global = new WebAssembly.Global(argument, {
    valueOf() {
      return 123n;
    }
  });
  assertEquals(global.value, 123n);
})();

Uncaught (in promise) SyntaxError: Cannot convert 0,0,152,204,169,171,54,197 to a BigInt

Running on Version 81.0.4004.0 (Developer Build) (64-bit) chromium. Also tried on latest stable chrome, and chromium. On Ubuntu 18.0.4.

With chrome://flags/#enable-experimental-webassembly-features for BigInt support for i64 types

I am opening the following page:
https://github.com/theblockstalk/eosio-sovereign-contract-poc/blob/1818f7f9233b7884aefbf1d909b083b28b9e1e59/sovereign/index.html

<!DOCTYPE html>
<html lang='en'>
  <head>
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
  </head>
  <body>
    <script>
      // Source C++ file for .wasm build:
      // https://github.com/theblockstalk/eosio-sovereign-contract-poc/tree/b726b0bc7f1881704f8c39ed40e623e39ff661da/contract
      //
      // This is compiled to wasm using the eosio.cdt tool. The eosio team has reported that the resulting wasm file is compatible with the wasm standard
      // https://github.com/EOSIO/eosio.cdt
      // This needs to be served over http. Source
      // https://github.com/theblockstalk/eosio-sovereign-contract-poc/blob/b726b0bc7f1881704f8c39ed40e623e39ff661da/build/sovereign.wasm
      const CONTRACT_WASM_FILE = fetch('../build/sovereign.wasm');
      let importObject = {
          env: {
              memoryBase: 0,
              memory: new WebAssembly.Memory({
                  initial: 256
              }),
              action_data_size: function() {
                  console.log('action_data_size');
                  const i32a = 11;
                  return i32a;
              },
              eosio_assert: function(i32a, i32b) {
                  console.log('eosio_assert');
                  console.log(i32a);
                  console.log(i32b);
              },
              memset: function(i32a, i32b, i32c) {
                  console.log('memset');
                  console.log(i32a);
                  console.log(i32b);
                  console.log(i32c);
                  const i32d = 12;
                  return i32d;
              },
              read_action_data: function(i32a, i32b) {
                  console.log('read_action_data');
                  console.log(i32a);
                  console.log(i32b);
                  const i32c = 13;
                  return i32c;
              },
              eosio_assert_code: function(condition /*i32*/, error_code /*i64*/) {
                  console.log('eosio_assert_code');
                  console.log(condition);
                  console.log(error_code);
              }
          }
      };
      const typedArray = new Uint8Array(CONTRACT_WASM_FILE);
      WebAssembly.instantiate(typedArray, importObject).then(module => {
        module.instance.exports.apply(
          Uint8Array("sovereign"),
          Uint8Array("dablockstalk"),
          Uint8Array("hi")
        );
      });
    </script>
  </body>
</html>

And getting the following error
index.js:61 Uncaught (in promise) SyntaxError: Cannot convert 0,0,152,204,169,171,54,197 to a BigInt

I'm not sure where the BigInt is being parsed as I cannot see any references to my code, or chromium code.

Propose to phase 4

Entry requirements:

  • Two or more Web VMs implement the feature.
    • SpiderMonkey and V8 implement.
  • At least one toolchain implements the feature.
  • The formalization and the reference interpreter are usually updated (though these two can be done as part of step 3 at the Working Group chair's discretion).
  • Community Group has reached consensus in support of the feature.

Valid values for imported i64 globals

In the current version of this proposal, any JavaScript object can be imported as an i64 global as long as ToBigInt64 does not throw an exception on that object. However, in the main WebAssembly spec we require at the moment that only JavaScript numbers are valid values for imported globals. I think this behavior was introduced to the WebAssembly spec with the mutable-globals proposal.
With this issue I wanted to flag this inconsistenc, so that we don't forget about it.

Propose to phase 3

Phase 3 entry requirements:

  • Test suite has been updated to cover the feature in its forked repo.
  • The test suite should run against some implementation, though it need not be the reference interpreter.
  • Formal notation in the spec need not be updated

During this phase, the following proceeds in parallel:

Embedders implement the feature.

  • The forked repo is updated to include revisions to the formalization.
  • The forked repo spec is updated to include implementation of the feature in the reference interpreter.
    • Sven: does not apply.
  • The feature is implemented in toolchains.
    • Sven: this doesn't apply to this proposal, brower behavior and polyfilling is difficult.

Implementation

Notes

  • Sven: #12 can be an addition to the spec

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.