diff --git a/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java b/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java index 2bcf9f493..315027d5f 100644 --- a/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java +++ b/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java @@ -387,6 +387,7 @@ public class JavaScriptTarget implements TeaVMTarget, TeaVMJavaScriptHost { if (!renderer.render(clsNodes)) { return; } + runtimeRenderer.renderHandWrittenRuntime("array.js"); renderer.renderStringPool(); renderer.renderStringConstants(); renderer.renderCompatibilityStubs(); diff --git a/core/src/main/resources/org/teavm/backend/javascript/array.js b/core/src/main/resources/org/teavm/backend/javascript/array.js new file mode 100644 index 000000000..6a4408610 --- /dev/null +++ b/core/src/main/resources/org/teavm/backend/javascript/array.js @@ -0,0 +1,47 @@ +/* + * Copyright 2019 Alexey Andreev. + * + * 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. + */ + +function $rt_array(cls, data) { + this.type = cls; + this.data = data; + this.$id$ = 0; + this.constructor = $rt_arraycls(cls); +} +$rt_array.prototype = Object.create($rt_objcls().prototype); +$rt_array.prototype.toString = function() { + var str = "["; + for (var i = 0; i < this.data.length; ++i) { + if (i > 0) { + str += ", "; + } + str += this.data[i].toString(); + } + str += "]"; + return str; +}; + +$rt_setCloneMethod($rt_array.prototype, function () { + var dataCopy; + if ('slice' in this.data) { + dataCopy = this.data.slice(); + } else { + dataCopy = new this.data.constructor(this.data.length); + for (var i = 0; i < dataCopy.length; ++i) { + dataCopy[i] = this.data[i]; + } + } + return new $rt_array(this.type, dataCopy); +}); \ No newline at end of file diff --git a/core/src/main/resources/org/teavm/backend/javascript/runtime.js b/core/src/main/resources/org/teavm/backend/javascript/runtime.js index 1695b0610..e03379023 100644 --- a/core/src/main/resources/org/teavm/backend/javascript/runtime.js +++ b/core/src/main/resources/org/teavm/backend/javascript/runtime.js @@ -43,7 +43,7 @@ function $rt_isAssignable(from, to) { } function $rt_createArray(cls, sz) { var data = new Array(sz); - var arr = new ($rt_arraycls(cls))(data); + var arr = new $rt_array(cls, data); if (sz > 0) { var i = 0; do { @@ -54,21 +54,21 @@ function $rt_createArray(cls, sz) { return arr; } function $rt_wrapArray(cls, data) { - return new ($rt_arraycls(cls))(data); + return new $rt_array(cls, data); } function $rt_createUnfilledArray(cls, sz) { - return new ($rt_arraycls(cls))(new Array(sz)); + return new $rt_array(cls, new Array(sz)); } function $rt_createLongArray(sz) { var data = new Array(sz); - var arr = new ($rt_arraycls($rt_longcls()))(data); + var arr = new $rt_array($rt_longcls(), data); for (var i = 0; i < sz; i = (i + 1) | 0) { data[i] = Long_ZERO; } return arr; } function $rt_createNumericArray(cls, nativeArray) { - return new ($rt_arraycls(cls))(nativeArray); + return new $rt_array(cls, nativeArray); } function $rt_createCharArray(sz) { return $rt_createNumericArray($rt_charcls(), new Uint16Array(sz)); @@ -95,35 +95,7 @@ function $rt_createDoubleArray(sz) { function $rt_arraycls(cls) { var result = cls.$array; if (result === null) { - var arraycls = function(data) { - this.data = data; - this.$id$ = 0; - }; - arraycls.prototype = new ($rt_objcls())(); - arraycls.prototype.constructor = arraycls; - arraycls.prototype.toString = function() { - var str = "["; - for (var i = 0; i < this.data.length; ++i) { - if (i > 0) { - str += ", "; - } - str += this.data[i].toString(); - } - str += "]"; - return str; - }; - $rt_setCloneMethod(arraycls.prototype, function () { - var dataCopy; - if ('slice' in this.data) { - dataCopy = this.data.slice(); - } else { - dataCopy = new this.data.constructor(this.data.length); - for (var i = 0; i < dataCopy.length; ++i) { - dataCopy[i] = this.data[i]; - } - } - return new arraycls(dataCopy); - }); + var arraycls = {}; var name = "[" + cls.$meta.binaryName; arraycls.$meta = { item : cls, supertypes : [$rt_objcls()], primitive : false, superclass : $rt_objcls(), name : name, binaryName : name, enum : false };