Adding JS polyfills to avoid issues because core-js does not deal well with the environment it's given.

This commit is contained in:
Ad5001 2024-09-27 02:34:42 +02:00
parent 850d076268
commit 8cefc56ac7
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
4 changed files with 132 additions and 4 deletions

View file

@ -16,6 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import js from "./lib/polyfills/js.mjs"
// Loading modules in order // Loading modules in order
import * as Objects from "./module/objects.mjs" import * as Objects from "./module/objects.mjs"
import * as ExprParser from "./module/expreval.mjs" import * as ExprParser from "./module/expreval.mjs"

View file

@ -482,7 +482,7 @@ export class ExprEvalExpression {
/** /**
* Calculates the value of the expression by giving all variables and their corresponding values. * Calculates the value of the expression by giving all variables and their corresponding values.
* @param {Object<string, number>} values * @param {Object<string, number|DrawableObject>} values
* @returns {number} * @returns {number}
*/ */
evaluate(values) { evaluate(values) {
@ -512,8 +512,7 @@ export class ExprEvalExpression {
* as constants or functions. * as constants or functions.
* @returns {string[]} * @returns {string[]}
*/ */
variables(options) { variables(options = {}) {
options = options || {}
const vars = [] const vars = []
getSymbols(this.tokens, vars, options) getSymbols(this.tokens, vars, options)
const functions = this.functions const functions = this.functions

View file

@ -0,0 +1,126 @@
/**
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions.
* Copyright (C) 2021-2024 Ad5001
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// JS polyfills to add because they're not implemented in the QML Scripting engine.
// CoreJS does not work well with it (as well as doubles the compiled size), so this is preferable.
function notPolyfilled(name) {
return function() { throw new Error(`${name} not polyfilled`) }
}
/**
* @param {number} depth
* @this {Array}
* @returns {Array}
*/
function arrayFlat(depth = 1) {
const newArray = []
for(const element of this) {
if(element instanceof Array)
newArray.push(...(depth > 1 ? element.flat(depth - 1) : element))
else
newArray.push(element)
}
return newArray
}
/**
* @param {function(any, number, Array): any} callbackFn
* @param {object} thisArg
* @this {Array}
* @returns {Array}
*/
function arrayFlatMap(callbackFn, thisArg) {
const newArray = []
for(let i = 0; i < this.length; i++) {
const value = callbackFn.call(thisArg ?? this, this[i], i, this)
if(value instanceof Array)
newArray.push(...value)
else
newArray.push(value)
}
return newArray
}
/**
* Replaces all instances of from by to.
* @param {string} from
* @param {string} to
* @this {string}
* @return {String}
*/
function stringReplaceAll(from, to) {
let str = this
while(str.includes(from))
str = str.replace(from, to)
return str
}
const polyfills = {
2017: [
[Object, "entries", notPolyfilled("Object.entries")],
[Object, "values", notPolyfilled("Object.values")],
[Object, "getOwnPropertyDescriptors", notPolyfilled("Object.getOwnPropertyDescriptors")],
[String.prototype, "padStart", notPolyfilled("String.prototype.padStart")],
[String.prototype, "padEnd", notPolyfilled("String.prototype.padEnd")]
],
2018: [
[Promise.prototype, "finally", notPolyfilled("Object.entries")]
],
2019: [
[String.prototype, "trimStart", notPolyfilled("String.prototype.trimStart")],
[String.prototype, "trimEnd", notPolyfilled("String.prototype.trimEnd")],
[Object, "fromEntries", notPolyfilled("Object.fromEntries")],
[Array.prototype, "flat", arrayFlat],
[Array.prototype, "flatMap", arrayFlatMap]
],
2020: [
[String.prototype, "matchAll", notPolyfilled("String.prototype.matchAll")],
[Promise, "allSettled", notPolyfilled("Promise.allSettled")]
],
2021: [
[Promise, "any", notPolyfilled("Promise.any")],
[String.prototype, "replaceAll", stringReplaceAll]
],
2022: [
[Array.prototype, "at", notPolyfilled("Array.prototype.at")],
[String.prototype, "at", notPolyfilled("String.prototype.at")],
[Object, "hasOwn", notPolyfilled("Object.hasOwn")]
],
2023: [
[Array.prototype, "findLast", notPolyfilled("Array.prototype.findLast")],
[Array.prototype, "toReversed", notPolyfilled("Array.prototype.toReversed")],
[Array.prototype, "toSorted", notPolyfilled("Array.prototype.toSorted")],
[Array.prototype, "toSpliced", notPolyfilled("Array.prototype.toSpliced")],
[Array.prototype, "with", notPolyfilled("Array.prototype.with")]
],
2024: [
[Object, "groupBy", notPolyfilled("Object.groupBy")],
[Map, "groupBy", notPolyfilled("Map.groupBy")]
]
}
// Fulfill polyfill.
for(const [year, entries] of Object.entries(polyfills)) {
const defined = entries.filter(x => x[0][x[1]] !== undefined)
console.info(`ES${year} support: ${defined.length === entries.length} (${defined.length}/${entries.length})`)
// Apply polyfills
for(const [context, functionName, polyfill] of entries.filter(x => x[0][x[1]] === undefined)) {
context[functionName] = polyfill
}
}

View file

@ -18,6 +18,7 @@
// Type polyfills for IDEs. // Type polyfills for IDEs.
// Never directly imported. // Never directly imported.
// Might need to be reimplemented in other implemententations.
Modules = Modules || {} Modules = Modules || {}
/** @type {function(string, string): string} */ /** @type {function(string, string): string} */
@ -25,7 +26,7 @@ qsTranslate = qsTranslate || function(category, string) { throw new Error('qsTra
/** @type {function(string): string} */ /** @type {function(string): string} */
qsTr = qsTr || function(string) { throw new Error('qsTr not implemented.'); } qsTr = qsTr || function(string) { throw new Error('qsTr not implemented.'); }
/** @type {function(string, string): string} */ /** @type {function(string, string): string} */
QT_TRANSLATE_NOOP = QT_TRANSLATE_NOOP || function(string, string) { throw new Error('QT_TRANSLATE_NOOP not implemented.'); } QT_TRANSLATE_NOOP = QT_TRANSLATE_NOOP || function(category, string) { throw new Error('QT_TRANSLATE_NOOP not implemented.'); }
/** @type {function(string): string} */ /** @type {function(string): string} */
QT_TR_NOOP = QT_TR_NOOP || function(string) { throw new Error('QT_TR_NOOP not implemented.'); } QT_TR_NOOP = QT_TR_NOOP || function(string) { throw new Error('QT_TR_NOOP not implemented.'); }
/** @type {function(string|boolean|int): string} */ /** @type {function(string|boolean|int): string} */