fix Date IO patch

This commit is contained in:
Dmitry Vasilev
2023-06-26 17:28:23 +03:00
parent 59cb2c9c6c
commit cd290ba1dd
2 changed files with 58 additions and 11 deletions

View File

@@ -23,9 +23,15 @@ const io_patch = (path, use_context = false) => {
}
const name = path.join('.')
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) {
// Try to finish fast
// 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 = () => {
@@ -226,13 +260,7 @@ export const apply_io_patches = () => {
// TODO patch setInterval to only cleanup all intervals on finish
const Date = cxt.window.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'])
patch_Date()
io_patch(['fetch'])
// Check if Response is defined, for node.js

View File

@@ -3358,4 +3358,23 @@ const y = x()`
const next_rnd = i.active_calltree_node.children[0].value
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',
)
}),
]