Introduced support for javascript Math and Kotlin Double to WASM32.

Implemented jsinterop tool for interaction with js math.
This commit is contained in:
Alexander Gorshenev
2017-12-28 12:33:29 +03:00
committed by alexander-gorshenev
parent 1399ac9777
commit f703877160
28 changed files with 990 additions and 187 deletions
+40 -12
View File
@@ -95,6 +95,24 @@ function toUTF16String(pointer, size) {
return string;
}
function twoIntsToDouble(upper, lower) {
var buffer = new ArrayBuffer(8);
var ints = new Int32Array(buffer);
var doubles = new Float64Array(buffer);
ints[1] = upper;
ints[0] = lower;
return doubles[0];
}
function doubleToTwoInts(value) {
var buffer = new ArrayBuffer(8);
var ints = new Int32Array(buffer);
var doubles = new Float64Array(buffer);
doubles[0] = value;
var twoInts = {upper: ints[1], lower: ints[0]};
return twoInts
}
function int32ToHeap(value, pointer) {
heap[pointer] = value & 0xff;
heap[pointer+1] = (value & 0xff00) >>> 8;
@@ -102,6 +120,11 @@ function int32ToHeap(value, pointer) {
heap[pointer+3] = (value & 0xff000000) >>> 24;
}
function doubleToReturnSlot(value) {
var twoInts = doubleToTwoInts(value);
instance.exports.ReturnSlot_setDouble(twoInts.upper, twoInts.lower);
}
function stackTop() {
// Read the value module's `__stack_pointer` is initialized with.
// It is the very first static in .data section.
@@ -239,17 +262,22 @@ function instantiateAndRunSync(arraybuffer, args) {
return invokeModule(instance, args)
}
if (isBrowser()) {
var filename = document.currentScript.getAttribute("wasm");
fetch(filename).then( function(response) {
return response.arrayBuffer();
}).then(function(arraybuffer) {
instantiateAndRun(arraybuffer, [filename]);
});
} else {
// Invoke from d8.
var arrayBuffer = readbuffer(arguments[0]);
var exitStatus = instantiateAndRunSync(arrayBuffer, arguments);
quit(exitStatus);
konan.moduleEntry = function (args) {
if (isBrowser()) {
if (!document.currentScript.hasAttribute("wasm")) {
throw new Error('Could not find the wasm attribute pointing to the WebAssembly binary.') ;
}
var filename = document.currentScript.getAttribute("wasm");
fetch(filename).then( function(response) {
return response.arrayBuffer();
}).then(function(arraybuffer) {
instantiateAndRun(arraybuffer, [filename]);
});
} else {
// Invoke from d8.
var arrayBuffer = readbuffer(args[0]);
var exitStatus = instantiateAndRunSync(arrayBuffer, args);
quit(exitStatus);
}
}
+35
View File
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* 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.
*/
#include "ReturnSlot.h"
#ifdef KONAN_WASM
namespace {
THREAD_LOCAL_VARIABLE long long storage;
}
extern "C" {
KDouble ReturnSlot_getDouble() {
return *reinterpret_cast<KDouble*>(&::storage);
}
void ReturnSlot_setDouble(KInt upper, KInt lower) {
reinterpret_cast<KInt*>(&::storage)[0] = lower;
reinterpret_cast<KInt*>(&::storage)[1] = upper;
}
}
#endif
+24
View File
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* 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.
*/
#include "Types.h"
#ifdef KONAN_WASM
extern "C" {
KDouble ReturnSlot_getDouble();
void ReturnSlot_setDouble(KInt upper, KInt lower);
}
#endif