mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 21:14:28 -08:00
WIP
This commit is contained in:
@@ -4,7 +4,7 @@ import {set_record_call} from './runtime.js'
|
|||||||
let cxt
|
let cxt
|
||||||
|
|
||||||
export const set_current_context = _cxt => {
|
export const set_current_context = _cxt => {
|
||||||
const should_apply_io_patches = cxt == null || cxt.run_window != _cxt.run_window
|
const should_apply_io_patches = cxt == null || cxt.window != _cxt.window
|
||||||
cxt = _cxt
|
cxt = _cxt
|
||||||
if(should_apply_io_patches) {
|
if(should_apply_io_patches) {
|
||||||
apply_io_patches()
|
apply_io_patches()
|
||||||
@@ -26,8 +26,11 @@ const io_patch = (path, use_context = false) => {
|
|||||||
|
|
||||||
const original = obj[method]
|
const original = obj[method]
|
||||||
obj[method] = function(...args) {
|
obj[method] = function(...args) {
|
||||||
|
|
||||||
|
// TODO does it work? After we change for global cxt? Review all cxt usages
|
||||||
if(cxt.io_cache_is_replay_aborted) {
|
if(cxt.io_cache_is_replay_aborted) {
|
||||||
// Try to finish fast
|
// Try to finish fast
|
||||||
|
// TODO invoke callback to notify that code must be restarted?
|
||||||
throw new Error('io replay aborted')
|
throw new Error('io replay aborted')
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,6 +43,8 @@ const io_patch = (path, use_context = false) => {
|
|||||||
: original.apply(this, args)
|
: original.apply(this, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cxt_copy = cxt
|
||||||
|
|
||||||
if(cxt.io_cache_is_recording) {
|
if(cxt.io_cache_is_recording) {
|
||||||
let ok, value, error
|
let ok, value, error
|
||||||
try {
|
try {
|
||||||
@@ -56,7 +61,7 @@ const io_patch = (path, use_context = false) => {
|
|||||||
// Patch callback
|
// Patch callback
|
||||||
const cb = args[0]
|
const cb = args[0]
|
||||||
args[0] = Object.defineProperty(function() {
|
args[0] = Object.defineProperty(function() {
|
||||||
if(cancelled) {
|
if(cxt_copy != cxt) {
|
||||||
// If code execution was cancelled, then never call callback
|
// If code execution was cancelled, then never call callback
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -77,7 +82,7 @@ const io_patch = (path, use_context = false) => {
|
|||||||
// TODO use cxt.promise_then, not finally which calls
|
// TODO use cxt.promise_then, not finally which calls
|
||||||
// patched 'then'?
|
// patched 'then'?
|
||||||
value = value.finally(() => {
|
value = value.finally(() => {
|
||||||
if(cancelled) {
|
if(cxt_copy != cxt) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if(cxt.io_cache_is_replay_aborted) {
|
if(cxt.io_cache_is_replay_aborted) {
|
||||||
@@ -145,7 +150,9 @@ const io_patch = (path, use_context = false) => {
|
|||||||
cxt.io_cache_resolver_is_set = true
|
cxt.io_cache_resolver_is_set = true
|
||||||
|
|
||||||
original_setTimeout(() => {
|
original_setTimeout(() => {
|
||||||
// TODO if called from prev execution, then throw to finish it ASAP
|
if(cxt_copy != cxt) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if(cxt.io_cache_is_replay_aborted) {
|
if(cxt.io_cache_is_replay_aborted) {
|
||||||
return
|
return
|
||||||
@@ -213,6 +220,13 @@ const io_patch = (path, use_context = false) => {
|
|||||||
// TODO bare IO functions should not be exposed at all, to allow calling it
|
// TODO bare IO functions should not be exposed at all, to allow calling it
|
||||||
// only from patched versions. Especially setInterval which can cause leaks
|
// only from patched versions. Especially setInterval which can cause leaks
|
||||||
export const apply_io_patches = () => {
|
export const apply_io_patches = () => {
|
||||||
|
// TODO remove, only for dev
|
||||||
|
// TODO test open_run_window
|
||||||
|
if(cxt.window.__io_patched) {
|
||||||
|
throw new Error('illegal state')
|
||||||
|
}
|
||||||
|
cxt.window.__io_patched = true
|
||||||
|
|
||||||
io_patch(['Math', 'random'])
|
io_patch(['Math', 'random'])
|
||||||
|
|
||||||
io_patch(['setTimeout'])
|
io_patch(['setTimeout'])
|
||||||
|
|||||||
@@ -96,8 +96,8 @@ const do_run = function*(module_fns, cxt, io_cache){
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const run = gen_to_promise(function*(module_fns, cxt, io_cache) {
|
export const run = gen_to_promise(function*(module_fns, cxt, io_cache) {
|
||||||
if(cxt.run_window != globalThis) {
|
if(cxt.window != globalThis) {
|
||||||
// TODO refactor, remove cxt.run_window
|
// TODO refactor, remove cxt.window
|
||||||
throw new Error('illegal state')
|
throw new Error('illegal state')
|
||||||
}
|
}
|
||||||
const result = yield* do_run(module_fns, cxt, io_cache)
|
const result = yield* do_run(module_fns, cxt, io_cache)
|
||||||
|
|||||||
@@ -3232,7 +3232,7 @@ const y = x()`
|
|||||||
assert_equal(state.eval_cxt.io_cache.length, 0)
|
assert_equal(state.eval_cxt.io_cache.length, 0)
|
||||||
}),
|
}),
|
||||||
|
|
||||||
test_only('record io discard prev execution', () => {
|
test('record io discard prev execution', () => {
|
||||||
// Populate cache
|
// Populate cache
|
||||||
const i = test_initial_state(`Math.random(0)`)
|
const i = test_initial_state(`Math.random(0)`)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user