Skip to content

Commit

Permalink
🐛 fixed Function.deserialize for asynchronous functions
Browse files Browse the repository at this point in the history
  • Loading branch information
FurryR committed Apr 3, 2024
1 parent 5f2b376 commit bec0724
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 17 deletions.
13 changes: 3 additions & 10 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,10 @@ jobs:
cache: 'npm'
- name: 🔽 Install dependencies
run: npm ci
- name: 🛠️ Build lpp (debug)
run: npm run dev --if-present
- name: ✅ Upload debug artifacts
uses: actions/upload-artifact@v3
with:
name: lpp-debug
path: dist/index.global.js
- name: 🛠️ Build lpp (release)
- name: 🛠️ Build lpp
run: npm run build --if-present
- name: ✅ Upload release artifacts
- name: ✅ Upload artifacts
uses: actions/upload-artifact@v3
with:
name: lpp-release
name: lpp
path: dist/index.global.js
248 changes: 248 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export class LppAsyncFunctionContext extends LppFunctionContext {
resolve: (value: LppValue | PromiseLike<LppValue>) => void
reject: (reason?: unknown) => void
}
await() {
detach() {
if (!this.promise) {
let resolveFn: (v: LppValue | PromiseLike<LppValue>) => void
let rejectFn: (reason: unknown) => void
Expand Down
19 changes: 13 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ import { LppBoundArg } from './impl/boundarg'
}
}
// Patch Function.
// TODO: These functions are deprecated and will be moved in future versions.
Global.Function.set(
'serialize',
LppFunction.native(({ args }) => {
Expand Down Expand Up @@ -421,12 +422,18 @@ import { LppBoundArg } from './impl/boundarg'
const Blocks = runtime.flyoutBlocks.constructor as BlocksConstructor
const blocks = new Blocks(runtime, true)
Serialization.deserializeBlock(blocks, val.script)
const blockId = val.script[val.block]?.id
const Target = runtime.getTargetForStage()
?.constructor as TargetConstructor
if (!Target) throw new Error('lpp: project is disposed')
return new LppReturn(
Metadata.attach(
new LppFunction(this.executeScratch.bind(this, Target)),
new LppFunction(
(blockId === 'constructAsyncFunction'
? this.executeScratchAsync
: this.executeScratch
).bind(this, Target)
),
new Serialization.ScratchMetadata(
val.signature,
[blocks, val.block],
Expand Down Expand Up @@ -722,7 +729,7 @@ import { LppBoundArg } from './impl/boundarg'
thenFn = then
thenSelf = new LppConstant(null)
}
lpp.await()
lpp.detach()
return ImmediatePromise.sync(
new ImmediatePromise<LppValue>(resolve => {
thenFn.apply(thenSelf, [
Expand Down Expand Up @@ -1118,22 +1125,22 @@ import { LppBoundArg } from './impl/boundarg'
let thenSelf: LppValue
if (then instanceof LppReference) {
if (!(then.value instanceof LppFunction)) {
lpp.await()
lpp.detach()
lpp.promise?.resolve(val)
return thread.stopThisScript()
}
thenFn = then.value
thenSelf = then.parent.deref() ?? new LppConstant(null)
} else {
if (!(then instanceof LppFunction)) {
lpp.await()
lpp.detach()
lpp.promise?.resolve(val)
return thread.stopThisScript()
}
thenFn = then
thenSelf = new LppConstant(null)
}
lpp.await()
lpp.detach()
return this.asap(
ImmediatePromise.sync(
new ImmediatePromise<void>(resolve => {
Expand Down Expand Up @@ -1535,7 +1542,7 @@ import { LppBoundArg } from './impl/boundarg'
// })
// Call callback (if exists) when the thread is finished.
controller.wait(thread).then(() => {
lpp.await()
lpp.detach()
lpp.promise?.resolve(new LppConstant(null))
})
})
Expand Down

0 comments on commit bec0724

Please sign in to comment.