Skip to content

[Draft] Experimenting with Soft Errors #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions asyncLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ class AsyncLogicEngine {
}

if (typeof this.methods[func] === 'object') {
const { asyncMethod, method, traverse } = this.methods[func]
const shouldTraverse = typeof traverse === 'undefined' ? true : traverse
const parsedData = shouldTraverse ? ((!data || typeof data !== 'object') ? [data] : coerceArray(await this.run(data, context, { above }))) : data
const { asyncMethod, method, lazy } = this.methods[func]
const parsedData = !lazy ? ((!data || typeof data !== 'object') ? [data] : coerceArray(await this.run(data, context, { above }))) : data
const result = await (asyncMethod || method)(parsedData, context, above, this)
return Array.isArray(result) ? Promise.all(result) : result
}
Expand All @@ -102,7 +101,7 @@ class AsyncLogicEngine {
/**
*
* @param {String} name The name of the method being added.
* @param {((args: any, context: any, above: any[], engine: AsyncLogicEngine) => any) | { traverse?: Boolean, method?: (args: any, context: any, above: any[], engine: AsyncLogicEngine) => any, asyncMethod?: (args: any, context: any, above: any[], engine: AsyncLogicEngine) => Promise<any>, deterministic?: Function | Boolean }} method
* @param {((args: any, context: any, above: any[], engine: AsyncLogicEngine) => any) | { lazy?: Boolean, traverse?: Boolean, method?: (args: any, context: any, above: any[], engine: AsyncLogicEngine) => any, asyncMethod?: (args: any, context: any, above: any[], engine: AsyncLogicEngine) => Promise<any>, deterministic?: Function | Boolean }} method
* @param {{ deterministic?: Boolean, async?: Boolean, sync?: Boolean, optimizeUnary?: boolean }} annotations This is used by the compiler to help determine if it can optimize the function being generated.
*/
addMethod (
Expand All @@ -115,9 +114,9 @@ class AsyncLogicEngine {
if (typeof async !== 'undefined') sync = !async

if (typeof method === 'function') {
if (async) method = { asyncMethod: method, traverse: true }
else method = { method, traverse: true }
} else method = { ...method }
if (async) method = { asyncMethod: method, lazy: false }
else method = { method, lazy: false }
} else method = { ...method, lazy: typeof method.traverse !== 'undefined' ? !method.traverse : method.lazy }

Object.assign(method, omitUndefined({ deterministic, optimizeUnary }))
// @ts-ignore
Expand Down Expand Up @@ -174,10 +173,10 @@ class AsyncLogicEngine {
// END OPTIMIZER BLOCK //

if (Array.isArray(logic)) {
const res = []
const res = new Array(logic.length)
// Note: In the past, it used .map and Promise.all; this can be changed in the future
// if we want it to run concurrently.
for (let i = 0; i < logic.length; i++) res.push(await this.run(logic[i], data, { above }))
for (let i = 0; i < logic.length; i++) res[i] = await this.run(logic[i], data, { above })
return res
}

Expand Down
2 changes: 1 addition & 1 deletion async_optimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function getMethod (logic, engine, methodName, above) {
const method = engine.methods[methodName]
const called = method.asyncMethod ? method.asyncMethod : method.method ? method.method : method

if (method.traverse === false) {
if (method.lazy) {
if (typeof method[Sync] === 'function' && method[Sync](logic, { engine })) {
const called = method.method ? method.method : method
return declareSync((data, abv) => called(logic[methodName], data, abv || above, engine.fallback), true)
Expand Down
3 changes: 2 additions & 1 deletion compatibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ const all = {
return oldAll.asyncMethod(args, context, above, engine)
},
deterministic: oldAll.deterministic,
traverse: oldAll.traverse
lazy: oldAll.lazy
}

function truthy (value) {
if (Array.isArray(value) && value.length === 0) return false
if (Number.isNaN(value)) return true
return value
}

Expand Down
1 change: 1 addition & 0 deletions compatible.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ for (const file of files) {
function correction (x) {
// eslint-disable-next-line no-compare-neg-zero
if (x === -0) return 0
if (Number.isNaN(x)) return { error: 'NaN' }
return x
}

Expand Down
13 changes: 7 additions & 6 deletions compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import asyncIterators from './async_iterators.js'
import { coerceArray } from './utilities/coerceArray.js'
import { countArguments } from './utilities/countArguments.js'
import { downgrade, precoerceNumber } from './utilities/downgrade.js'

/**
* Provides a simple way to compile logic into a function that can be run.
Expand Down Expand Up @@ -87,7 +88,7 @@ export function isDeterministic (method, engine, buildState) {
if (lower === undefined) return true
if (!engine.methods[func]) throw new Error(`Method '${func}' was not found in the Logic Engine.`)

if (engine.methods[func].traverse === false) {
if (engine.methods[func].lazy) {
return typeof engine.methods[func].deterministic === 'function'
? engine.methods[func].deterministic(lower, buildState)
: engine.methods[func].deterministic
Expand Down Expand Up @@ -118,7 +119,7 @@ function isDeepSync (method, engine) {
const lower = method[func]
if (!isSync(engine.methods[func])) return false

if (engine.methods[func].traverse === false) {
if (engine.methods[func].lazy) {
if (typeof engine.methods[func][Sync] === 'function' && engine.methods[func][Sync](method, { engine })) return true
return false
}
Expand Down Expand Up @@ -193,7 +194,7 @@ function buildString (method, buildState = {}) {
}

let lower = method[func]
if (!lower || typeof lower !== 'object') lower = [lower]
if ((!lower || typeof lower !== 'object') && (!engine.methods[func].lazy)) lower = [lower]

if (engine.methods[func] && engine.methods[func].compile) {
let str = engine.methods[func].compile(lower, buildState)
Expand All @@ -219,7 +220,7 @@ function buildString (method, buildState = {}) {
const argCount = countArguments(asyncDetected ? engine.methods[func].asyncMethod : engine.methods[func].method)
const argumentsNeeded = argumentsDict[argCount - 1] || argumentsDict[2]

if (engine.methods[func] && (typeof engine.methods[func].traverse === 'undefined' ? true : engine.methods[func].traverse)) {
if (engine.methods[func] && !engine.methods[func].lazy) {
return makeAsync(`engine.methods["${func}"]${asyncDetected ? '.asyncMethod' : '.method'}(${coerce}(` + buildString(lower, buildState) + ')' + argumentsNeeded + ')')
} else {
notTraversed.push(lower)
Expand Down Expand Up @@ -307,12 +308,12 @@ function processBuiltString (method, str, buildState) {
str = str.replace(`__%%%${x}%%%__`, item)
})

const final = `(values, methods, notTraversed, asyncIterators, engine, above, coerceArray) => ${buildState.asyncDetected ? 'async' : ''} (context ${buildState.extraArguments ? ',' + buildState.extraArguments : ''}) => { let prev; const result = ${str}; return result }`
const final = `(values, methods, notTraversed, asyncIterators, engine, above, coerceArray, downgrade, precoerceNumber) => ${buildState.asyncDetected ? 'async' : ''} (context ${buildState.extraArguments ? ',' + buildState.extraArguments : ''}) => { ${str.includes('prev') ? 'let prev;' : ''} const result = ${str}; return result }`
// console.log(str)
// console.log(final)
// eslint-disable-next-line no-eval
return Object.assign(
(typeof globalThis !== 'undefined' ? globalThis : global).eval(final)(values, methods, notTraversed, asyncIterators, engine, above, coerceArray), {
(typeof globalThis !== 'undefined' ? globalThis : global).eval(final)(values, methods, notTraversed, asyncIterators, engine, above, coerceArray, downgrade, precoerceNumber), {
[Sync]: !buildState.asyncDetected,
aboveDetected: typeof str === 'string' && str.includes(', above')
})
Expand Down
Loading
Loading