mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 21:14:28 -08:00
fix Date IO patch
This commit is contained in:
@@ -23,9 +23,15 @@ const io_patch = (path, use_context = false) => {
|
|||||||
}
|
}
|
||||||
const name = path.join('.')
|
const name = path.join('.')
|
||||||
|
|
||||||
|
|
||||||
const original = obj[method]
|
const original = obj[method]
|
||||||
obj[method] = function(...args) {
|
|
||||||
|
obj[method] = make_patched_method(original, name, use_context)
|
||||||
|
|
||||||
|
obj[method].__original = original
|
||||||
|
}
|
||||||
|
|
||||||
|
const make_patched_method = (original, name, use_context) => {
|
||||||
|
const method = function(...args) {
|
||||||
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?
|
// TODO invoke callback to notify that code must be restarted?
|
||||||
@@ -210,9 +216,37 @@ const io_patch = (path, use_context = false) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(obj[method], 'name', {value: original.name})
|
Object.defineProperty(method, 'name', {value: original.name})
|
||||||
|
|
||||||
obj[method].__original = original
|
return method
|
||||||
|
}
|
||||||
|
|
||||||
|
const patch_Date = () => {
|
||||||
|
const Date = cxt.window.Date
|
||||||
|
const Date_patched = make_patched_method(Date, 'Date', false)
|
||||||
|
cxt.window.Date = function(...args) {
|
||||||
|
if(args.length == 0) {
|
||||||
|
// return current Date, IO operation
|
||||||
|
if(new.target != null) {
|
||||||
|
return new Date_patched(...args)
|
||||||
|
} else {
|
||||||
|
return Date_patched(...args)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// pure function
|
||||||
|
if(new.target != null) {
|
||||||
|
return new Date(...args)
|
||||||
|
} else {
|
||||||
|
return Date(...args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cxt.window.Date.__original = Date
|
||||||
|
|
||||||
|
cxt.window.Date.parse = Date.parse
|
||||||
|
cxt.window.Date.now = Date.now
|
||||||
|
cxt.window.Date.UTC = Date.UTC
|
||||||
|
io_patch(['Date', 'now'])
|
||||||
}
|
}
|
||||||
|
|
||||||
export const apply_io_patches = () => {
|
export const apply_io_patches = () => {
|
||||||
@@ -226,13 +260,7 @@ export const apply_io_patches = () => {
|
|||||||
|
|
||||||
// TODO patch setInterval to only cleanup all intervals on finish
|
// TODO patch setInterval to only cleanup all intervals on finish
|
||||||
|
|
||||||
const Date = cxt.window.Date
|
patch_Date()
|
||||||
io_patch(['Date'])
|
|
||||||
cxt.window.Date.parse = Date.parse
|
|
||||||
cxt.window.Date.now = Date.now
|
|
||||||
cxt.window.Date.UTC = Date.UTC
|
|
||||||
io_patch(['Date', 'now'])
|
|
||||||
|
|
||||||
|
|
||||||
io_patch(['fetch'])
|
io_patch(['fetch'])
|
||||||
// Check if Response is defined, for node.js
|
// Check if Response is defined, for node.js
|
||||||
|
|||||||
19
test/test.js
19
test/test.js
@@ -3358,4 +3358,23 @@ const y = x()`
|
|||||||
const next_rnd = i.active_calltree_node.children[0].value
|
const next_rnd = i.active_calltree_node.children[0].value
|
||||||
assert_equal(rnd, next_rnd)
|
assert_equal(rnd, next_rnd)
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
test('record io Date', () => {
|
||||||
|
assert_equal(
|
||||||
|
test_initial_state('new Date()').io_cache.length,
|
||||||
|
1
|
||||||
|
)
|
||||||
|
assert_equal(
|
||||||
|
test_initial_state('new Date("2020-01-01")').io_cache,
|
||||||
|
undefined,
|
||||||
|
)
|
||||||
|
assert_equal(
|
||||||
|
typeof(test_initial_state('Date()').io_cache[0].value),
|
||||||
|
'string',
|
||||||
|
)
|
||||||
|
assert_equal(
|
||||||
|
typeof(test_initial_state('new Date()').io_cache[0].value),
|
||||||
|
'object',
|
||||||
|
)
|
||||||
|
}),
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user