[js-interop] Include JS interop into stdlib
This commit is contained in:
@@ -1,39 +1,20 @@
|
|||||||
import org.jetbrains.kotlin.KlibInstall
|
/*
|
||||||
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
apply plugin: 'konan'
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
apply from: "$rootDir/gradle/kotlinGradlePlugin.gradle"
|
||||||
mavenCentral()
|
|
||||||
maven {
|
|
||||||
url kotlinCompilerRepo
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin"
|
|
||||||
}
|
|
||||||
|
|
||||||
ext.konanHome = distDir.absolutePath
|
|
||||||
ext.jvmArgs = project.findProperty("platformLibsJvmArgs") ?: "-Xmx3G"
|
|
||||||
ext.setProperty("konan.home", konanHome)
|
|
||||||
ext.setProperty("konan.jvmArgs", jvmArgs)
|
|
||||||
}
|
|
||||||
|
|
||||||
konan.targets = [ 'wasm32' ]
|
|
||||||
|
|
||||||
konanArtifacts {
|
|
||||||
library("jsinterop") {
|
|
||||||
dependsOn ':wasm32CrossDist' // our plugin runs konanc, so make sure it is already there.
|
|
||||||
extraOpts '-includeBinary', project.file('src/main/js/jsinterop.js')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
task installJsRuntime(type: KlibInstall) {
|
|
||||||
dependsOn konanArtifacts.jsinterop.wasm32
|
|
||||||
klib = konanArtifacts.jsinterop.wasm32.artifact
|
|
||||||
repo = file("$konanHome/klib/platform/wasm32")
|
|
||||||
it.target = 'wasm32'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 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.
|
||||||
|
*/
|
||||||
|
|
||||||
konan.libraries.push ({
|
konan.libraries.push ({
|
||||||
arenas: new Map(),
|
arenas: new Map(),
|
||||||
|
|||||||
@@ -1,3 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 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.
|
||||||
|
*/
|
||||||
|
|
||||||
package kotlinx.wasm.jsinterop
|
package kotlinx.wasm.jsinterop
|
||||||
|
|
||||||
import konan.internal.ExportForCppRuntime
|
import konan.internal.ExportForCppRuntime
|
||||||
@@ -26,9 +42,13 @@ external public fun pushIntToArena(arena: Arena, value: Int)
|
|||||||
|
|
||||||
const val upperWord = 0xffffffff.toLong() shl 32
|
const val upperWord = 0xffffffff.toLong() shl 32
|
||||||
|
|
||||||
|
@Used
|
||||||
|
@ExportForCppRuntime
|
||||||
fun doubleUpper(value: Double): Int =
|
fun doubleUpper(value: Double): Int =
|
||||||
((value.toBits() and upperWord) ushr 32) .toInt()
|
((value.toBits() and upperWord) ushr 32) .toInt()
|
||||||
|
|
||||||
|
@Used
|
||||||
|
@ExportForCppRuntime
|
||||||
fun doubleLower(value: Double): Int =
|
fun doubleLower(value: Double): Int =
|
||||||
(value.toBits() and 0x00000000ffffffff) .toInt()
|
(value.toBits() and 0x00000000ffffffff) .toInt()
|
||||||
|
|
||||||
@@ -61,7 +81,7 @@ fun runLambda(pointer: Int, argumentsArena: Arena, argumentsArenaSize: Int): Int
|
|||||||
val previousArena = ArenaManager.currentArena
|
val previousArena = ArenaManager.currentArena
|
||||||
ArenaManager.currentArena = argumentsArena
|
ArenaManager.currentArena = argumentsArena
|
||||||
// TODO: LP64 unsafe: wasm32 passes Int, not Long.
|
// TODO: LP64 unsafe: wasm32 passes Int, not Long.
|
||||||
val func = StableRef.fromValue(pointer.toLong().toCPointer()!!).get() as KtFunction<JsValue>
|
val func = pointer.toLong().toCPointer<CPointed>()!!.asStableRef<KtFunction<JsValue>>().get()
|
||||||
val result = func(arguments)
|
val result = func(arguments)
|
||||||
|
|
||||||
ArenaManager.currentArena = previousArena
|
ArenaManager.currentArena = previousArena
|
||||||
@@ -124,3 +144,7 @@ object ArenaManager {
|
|||||||
val globalArena = allocateArena()
|
val globalArena = allocateArena()
|
||||||
var currentArena = globalArena
|
var currentArena = globalArena
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Used
|
||||||
|
@ExportForCppRuntime("Konan_js_getCurrentArena")
|
||||||
|
internal fun getCurrentArena() = ArenaManager.currentArena
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2017 JetBrains s.r.o.
|
* Copyright 2010-2018 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -192,10 +192,12 @@ targetList.each { target ->
|
|||||||
'-produce', 'library', '-module_name', 'stdlib',
|
'-produce', 'library', '-module_name', 'stdlib',
|
||||||
project(':Interop:Runtime').file('src/main/kotlin'),
|
project(':Interop:Runtime').file('src/main/kotlin'),
|
||||||
project(':Interop:Runtime').file('src/native/kotlin'),
|
project(':Interop:Runtime').file('src/native/kotlin'),
|
||||||
|
project(':Interop:JsRuntime').file('src/main/kotlin'),
|
||||||
project(':runtime').file('src/main/kotlin')]
|
project(':runtime').file('src/main/kotlin')]
|
||||||
inputs.dir(project(':runtime').file('src/main/kotlin'))
|
inputs.dir(project(':runtime').file('src/main/kotlin'))
|
||||||
inputs.dir(project(':Interop:Runtime').file('src/main/kotlin'))
|
inputs.dir(project(':Interop:Runtime').file('src/main/kotlin'))
|
||||||
inputs.dir(project(':Interop:Runtime').file('src/native/kotlin'))
|
inputs.dir(project(':Interop:Runtime').file('src/native/kotlin'))
|
||||||
|
inputs.dir(project(':Interop:JsRuntime').file('src/main/kotlin'))
|
||||||
outputs.dir(project(':runtime').file("build/${target}Stdlib"))
|
outputs.dir(project(':runtime').file("build/${target}Stdlib"))
|
||||||
|
|
||||||
dependsOn ":runtime:${target}Runtime"
|
dependsOn ":runtime:${target}Runtime"
|
||||||
|
|||||||
+4
-2
@@ -291,8 +291,10 @@ targetList.each { target ->
|
|||||||
into("konan/targets/$target/native")
|
into("konan/targets/$target/native")
|
||||||
}
|
}
|
||||||
if (target == 'wasm32') {
|
if (target == 'wasm32') {
|
||||||
from(project(':runtime').file('src/launcher/js')) {
|
into("$stdlib/targets/wasm32/included") {
|
||||||
into("$stdlib/targets/wasm32/included")
|
from(project(':runtime').file('src/main/js'))
|
||||||
|
from(project(':runtime').file('src/launcher/js'))
|
||||||
|
from(project(':Interop:JsRuntime').file('src/main/js'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2017 JetBrains s.r.o.
|
* Copyright 2010-2018 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef RUNTIME_DOUBLECONVERSIONS_H
|
||||||
|
#define RUNTIME_DOUBLECONVERSIONS_H
|
||||||
|
|
||||||
|
#include "Types.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
KLong l;
|
||||||
|
KDouble d;
|
||||||
|
} DoubleAlias;
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
KInt i;
|
||||||
|
KFloat f;
|
||||||
|
} FloatAlias;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
inline KDouble bitsToDouble(KLong bits) {
|
||||||
|
DoubleAlias alias;
|
||||||
|
alias.l = bits;
|
||||||
|
return alias.d;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline KLong doubleToBits(KDouble value) {
|
||||||
|
DoubleAlias alias;
|
||||||
|
alias.d = value;
|
||||||
|
return alias.l;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline KFloat bitsToFloat(KInt bits) {
|
||||||
|
FloatAlias alias;
|
||||||
|
alias.i = bits;
|
||||||
|
return alias.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline KInt floatToBits(KFloat value) {
|
||||||
|
FloatAlias alias;
|
||||||
|
alias.f = value;
|
||||||
|
return alias.i;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" KInt doubleUpper(KDouble value);
|
||||||
|
extern "C" KInt doubleLower(KDouble value);
|
||||||
|
|
||||||
|
#endif // RUNTIME_DOUBLECONVERSIONS_H
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 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"
|
||||||
|
|
||||||
|
#ifndef KONAN_WASM
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
// These functions are implemented in JS file for WASM and are not available on other platforms.
|
||||||
|
RUNTIME_NORETURN JS::Arena Konan_js_allocateArena() {
|
||||||
|
RuntimeAssert(false, "JavaScript interop is disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
RUNTIME_NORETURN void Konan_js_freeArena(JS::Arena arena) {
|
||||||
|
RuntimeAssert(false, "JavaScript interop is disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
RUNTIME_NORETURN void Konan_js_pushIntToArena(JS::Arena arena, KInt value) {
|
||||||
|
RuntimeAssert(false, "JavaScript interop is disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
RUNTIME_NORETURN KInt Konan_js_getProperty(JS::Arena arena,
|
||||||
|
JS::Object obj,
|
||||||
|
JS::Pointer propertyPtr,
|
||||||
|
KInt propertyLen) {
|
||||||
|
RuntimeAssert(false, "JavaScript interop is disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
RUNTIME_NORETURN void Konan_js_setFunction(JS::Arena arena,
|
||||||
|
JS::Object obj,
|
||||||
|
JS::Pointer propertyName,
|
||||||
|
KInt propertyLength,
|
||||||
|
KInt function) {
|
||||||
|
RuntimeAssert(false, "JavaScript interop is disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
RUNTIME_NORETURN void Konan_js_setString(JS::Arena arena,
|
||||||
|
JS::Object obj,
|
||||||
|
JS::Pointer propertyName,
|
||||||
|
KInt propertyLength,
|
||||||
|
JS::Pointer stringPtr,
|
||||||
|
KInt stringLength) {
|
||||||
|
RuntimeAssert(false, "JavaScript interop is disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
}; // extern "C"
|
||||||
|
|
||||||
|
#endif // #ifndef KONAN_WASM
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2017 JetBrains s.r.o.
|
* Copyright 2010-2018 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
|
#include "DoubleConversions.h"
|
||||||
#include "Natives.h"
|
#include "Natives.h"
|
||||||
#include "Exceptions.h"
|
#include "Exceptions.h"
|
||||||
|
|
||||||
@@ -381,23 +382,8 @@ KDouble Kotlin_Float_toDouble (KFloat a ) { return a; }
|
|||||||
KByte Kotlin_Float_toByte (KFloat a ) { return (KByte) Kotlin_Float_toInt(a); }
|
KByte Kotlin_Float_toByte (KFloat a ) { return (KByte) Kotlin_Float_toInt(a); }
|
||||||
KShort Kotlin_Float_toShort (KFloat a ) { return (KShort) Kotlin_Float_toInt(a); }
|
KShort Kotlin_Float_toShort (KFloat a ) { return (KShort) Kotlin_Float_toInt(a); }
|
||||||
|
|
||||||
KInt Kotlin_Float_bits (KFloat a) {
|
KInt Kotlin_Float_bits (KFloat a ) { return floatToBits(a); }
|
||||||
union {
|
KFloat Kotlin_Float_fromBits (KInt a ) { return bitsToFloat(a); }
|
||||||
KFloat f;
|
|
||||||
KInt i;
|
|
||||||
} alias;
|
|
||||||
alias.f = a;
|
|
||||||
return alias.i;
|
|
||||||
}
|
|
||||||
|
|
||||||
KFloat Kotlin_Float_fromBits (KInt a) {
|
|
||||||
union {
|
|
||||||
KFloat f;
|
|
||||||
KInt i;
|
|
||||||
} alias;
|
|
||||||
alias.i = a;
|
|
||||||
return alias.f;
|
|
||||||
}
|
|
||||||
|
|
||||||
KBoolean Kotlin_Float_isNaN (KFloat a) { return isnan(a); }
|
KBoolean Kotlin_Float_isNaN (KFloat a) { return isnan(a); }
|
||||||
KBoolean Kotlin_Float_isInfinite (KFloat a) { return isinf(a); }
|
KBoolean Kotlin_Float_isInfinite (KFloat a) { return isinf(a); }
|
||||||
@@ -461,23 +447,8 @@ KLong Kotlin_Double_toLong (KDouble a ) {
|
|||||||
KFloat Kotlin_Double_toFloat (KDouble a ) { return a; }
|
KFloat Kotlin_Double_toFloat (KDouble a ) { return a; }
|
||||||
KDouble Kotlin_Double_toDouble (KDouble a ) { return a; }
|
KDouble Kotlin_Double_toDouble (KDouble a ) { return a; }
|
||||||
|
|
||||||
KLong Kotlin_Double_bits (KDouble a) {
|
KLong Kotlin_Double_bits (KDouble a ) { return doubleToBits(a); }
|
||||||
union {
|
KDouble Kotlin_Double_fromBits (KLong a ) { return bitsToDouble(a); }
|
||||||
KDouble d;
|
|
||||||
KLong l;
|
|
||||||
} alias;
|
|
||||||
alias.d = a;
|
|
||||||
return alias.l;
|
|
||||||
}
|
|
||||||
|
|
||||||
KDouble Kotlin_Double_fromBits (KLong a) {
|
|
||||||
union {
|
|
||||||
KDouble d;
|
|
||||||
KLong l;
|
|
||||||
} alias;
|
|
||||||
alias.l = a;
|
|
||||||
return alias.d;
|
|
||||||
}
|
|
||||||
|
|
||||||
KBoolean Kotlin_Double_isNaN (KDouble a) { return isnan(a); }
|
KBoolean Kotlin_Double_isNaN (KDouble a) { return isnan(a); }
|
||||||
KBoolean Kotlin_Double_isInfinite (KDouble a) { return isinf(a); }
|
KBoolean Kotlin_Double_isInfinite (KDouble a) { return isinf(a); }
|
||||||
|
|||||||
@@ -50,6 +50,12 @@ typedef ObjHeader* KRef;
|
|||||||
typedef const ObjHeader* KConstRef;
|
typedef const ObjHeader* KConstRef;
|
||||||
typedef const ArrayHeader* KString;
|
typedef const ArrayHeader* KString;
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
typedef KInt Arena;
|
||||||
|
typedef KInt Object;
|
||||||
|
typedef KInt Pointer;
|
||||||
|
}
|
||||||
|
|
||||||
// Definitions of STL classes used inside Konan runtime.
|
// Definitions of STL classes used inside Konan runtime.
|
||||||
typedef std::basic_string<char, std::char_traits<char>,
|
typedef std::basic_string<char, std::char_traits<char>,
|
||||||
KonanAllocator<char>> KStdString;
|
KonanAllocator<char>> KStdString;
|
||||||
|
|||||||
Reference in New Issue
Block a user