module coracle import nspkg as facade import os struct CallbackState { mut: calls []string } fn callback_mark(context voidptr, name string) { mut state := unsafe { &CallbackState(context) } state.calls << name } fn callback_log(context voidptr, level LogLevel, message &u8, arguments voidptr) { if unsafe { cstring_to_vstring(message) } != 'cb-test' { return } assert level == .warning && arguments == unsafe { nil } callback_mark(context, 'log') } fn callback_download(context voidptr, filename &u8, kind DownloadEventType, data voidptr) { assert unsafe { cstring_to_vstring(filename) } == 'file' assert kind == .progress && data == unsafe { nil } callback_mark(context, 'download') } fn callback_fetch(context voidptr, url &u8, directory &u8, force i32) i32 { assert unsafe { cstring_to_vstring(url) } == 'url' assert unsafe { cstring_to_vstring(directory) } == 'directory' assert force == 0 callback_mark(context, 'fetch') return 42 } fn callback_event(context voidptr, event &Event) { assert event == unsafe { nil } callback_mark(context, 'event') } fn callback_question(context voidptr, question &Question) { assert question == unsafe { nil } callback_mark(context, 'question') } fn callback_progress(context voidptr, kind Progress, name &u8, percent i32, total uint, current uint) { assert kind == .add_start && percent == 42 && total == 9 && current == 3 assert unsafe { cstring_to_vstring(name) } == 'pkg' callback_mark(context, 'progress') } fn callback_public_log(context voidptr, level facade.LogLevel, message &u8, arguments voidptr) { if unsafe { cstring_to_vstring(message) } != 'cb-test' { return } assert level == .warning && arguments == unsafe { nil } callback_mark(context, 'log') } fn callback_public_download(context voidptr, filename &u8, kind facade.DownloadEventType, data voidptr) { assert unsafe { cstring_to_vstring(filename) } == 'file' assert kind == .progress && data == unsafe { nil } callback_mark(context, 'download') } fn callback_public_event(context voidptr, event &facade.Event) { assert event == unsafe { nil } callback_mark(context, 'event') } fn callback_public_question(context voidptr, question &facade.Question) { assert question == unsafe { nil } callback_mark(context, 'question') } fn callback_public_progress(context voidptr, kind facade.Progress, name &u8, percent i32, total uint, current uint) { assert kind == .add_start && percent == 42 && total == 9 && current == 3 assert unsafe { cstring_to_vstring(name) } == 'pkg' callback_mark(context, 'progress') } fn callback_reference_read(handle &Handle, kind string, context voidptr, expected voidptr) !string { if _ := handle.register_syncdb('', 0) { assert false } value := match kind { 'log' { voidptr(handle.option_get_logcb()) } 'download' { voidptr(handle.option_get_dlcb()) } 'fetch' { voidptr(handle.option_get_fetchcb()) } 'event' { voidptr(handle.option_get_eventcb()) } 'question' { voidptr(handle.option_get_questioncb()) } else { voidptr(handle.option_get_progresscb()) } } code := int(handle.errno()) if _ := handle.register_syncdb('', 0) { assert false } actual_context := match kind { 'log' { handle.option_get_logcb_ctx() } 'download' { handle.option_get_dlcb_ctx() } 'fetch' { handle.get_fetchcb_ctx() } 'event' { handle.option_get_eventcb_ctx() } 'question' { handle.option_get_questioncb_ctx() } else { handle.option_get_progresscb_ctx() } } assert actual_context == context assert value == expected return '${kind}:${value != unsafe { nil }}:${actual_context != unsafe { nil }}:${code}:${int(handle.errno())}' } fn callback_public_read(handle &facade.Handle, kind string, context voidptr, expected voidptr) !string { if _ := handle.register_syncdb('', 0) { assert false } value := match kind { 'log' { voidptr(handle.option_get_logcb()) } 'download' { voidptr(handle.option_get_dlcb()) } 'fetch' { voidptr(handle.option_get_fetchcb()) } 'event' { voidptr(handle.option_get_eventcb()) } 'question' { voidptr(handle.option_get_questioncb()) } else { voidptr(handle.option_get_progresscb()) } } code := int(handle.errno()) if _ := handle.register_syncdb('', 0) { assert false } actual_context := match kind { 'log' { handle.option_get_logcb_ctx() } 'download' { handle.option_get_dlcb_ctx() } 'fetch' { handle.get_fetchcb_ctx() } 'event' { handle.option_get_eventcb_ctx() } 'question' { handle.option_get_questioncb_ctx() } else { handle.option_get_progresscb_ctx() } } assert actual_context == context assert value == expected return '${kind}:${value != unsafe { nil }}:${actual_context != unsafe { nil }}:${code}:${int(handle.errno())}' } fn callback_reference_cycle(handle &Handle, kind string, first voidptr, second voidptr) ![]string { mut trace := [callback_reference_read(handle, kind, unsafe { nil }, unsafe { nil })!] match kind { 'log' { handle.option_set_logcb(callback_log, first)! } 'download' { handle.option_set_dlcb(callback_download, first)! } 'fetch' { handle.option_set_fetchcb(callback_fetch, first)! } 'event' { handle.option_set_eventcb(callback_event, first)! } 'question' { handle.option_set_questioncb(callback_question, first)! } else { handle.option_set_progresscb(callback_progress, first)! } } value := match kind { 'log' { voidptr(handle.option_get_logcb()) } 'download' { voidptr(handle.option_get_dlcb()) } 'fetch' { voidptr(handle.option_get_fetchcb()) } 'event' { voidptr(handle.option_get_eventcb()) } 'question' { voidptr(handle.option_get_questioncb()) } else { voidptr(handle.option_get_progresscb()) } } assert value != unsafe { nil } trace << callback_reference_read(handle, kind, first, value)! match kind { 'log' { handle.option_set_logcb(handle.option_get_logcb(), second)! cb := handle.option_get_logcb() cb(second, .warning, c'cb-test', unsafe { nil }) } 'download' { handle.option_set_dlcb(handle.option_get_dlcb(), second)! cb := handle.option_get_dlcb() cb(second, c'file', .progress, unsafe { nil }) } 'fetch' { handle.option_set_fetchcb(handle.option_get_fetchcb(), second)! cb := handle.option_get_fetchcb() assert cb(second, c'url', c'directory', 0) == 42 } 'event' { handle.option_set_eventcb(handle.option_get_eventcb(), second)! cb := handle.option_get_eventcb() cb(second, unsafe { nil }) } 'question' { handle.option_set_questioncb(handle.option_get_questioncb(), second)! cb := handle.option_get_questioncb() cb(second, unsafe { nil }) } else { handle.option_set_progresscb(handle.option_get_progresscb(), second)! cb := handle.option_get_progresscb() cb(second, .add_start, c'pkg', 42, 9, 3) } } trace << callback_reference_read(handle, kind, second, value)! match kind { 'log' { handle.option_set_logcb(unsafe { nil }, second)! } 'download' { handle.option_set_dlcb(unsafe { nil }, second)! } 'fetch' { handle.option_set_fetchcb(unsafe { nil }, second)! } 'event' { handle.option_set_eventcb(unsafe { nil }, second)! } 'question' { handle.option_set_questioncb(unsafe { nil }, second)! } else { handle.option_set_progresscb(unsafe { nil }, second)! } } trace << callback_reference_read(handle, kind, second, unsafe { nil })! return trace } fn callback_public_cycle(handle &facade.Handle, kind string, first voidptr, second voidptr) ![]string { mut trace := [callback_public_read(handle, kind, unsafe { nil }, unsafe { nil })!] match kind { 'log' { handle.option_set_logcb(callback_public_log, first)! } 'download' { handle.option_set_dlcb(callback_public_download, first)! } 'fetch' { handle.option_set_fetchcb(callback_fetch, first)! } 'event' { handle.option_set_eventcb(callback_public_event, first)! } 'question' { handle.option_set_questioncb(callback_public_question, first)! } else { handle.option_set_progresscb(callback_public_progress, first)! } } value := match kind { 'log' { voidptr(handle.option_get_logcb()) } 'download' { voidptr(handle.option_get_dlcb()) } 'fetch' { voidptr(handle.option_get_fetchcb()) } 'event' { voidptr(handle.option_get_eventcb()) } 'question' { voidptr(handle.option_get_questioncb()) } else { voidptr(handle.option_get_progresscb()) } } assert value != unsafe { nil } trace << callback_public_read(handle, kind, first, value)! match kind { 'log' { handle.option_set_logcb(handle.option_get_logcb(), second)! cb := handle.option_get_logcb() cb(second, .warning, c'cb-test', unsafe { nil }) } 'download' { handle.option_set_dlcb(handle.option_get_dlcb(), second)! cb := handle.option_get_dlcb() cb(second, c'file', .progress, unsafe { nil }) } 'fetch' { handle.option_set_fetchcb(handle.option_get_fetchcb(), second)! cb := handle.option_get_fetchcb() assert cb(second, c'url', c'directory', 0) == 42 } 'event' { handle.option_set_eventcb(handle.option_get_eventcb(), second)! cb := handle.option_get_eventcb() cb(second, unsafe { nil }) } 'question' { handle.option_set_questioncb(handle.option_get_questioncb(), second)! cb := handle.option_get_questioncb() cb(second, unsafe { nil }) } else { handle.option_set_progresscb(handle.option_get_progresscb(), second)! cb := handle.option_get_progresscb() cb(second, .add_start, c'pkg', 42, 9, 3) } } trace << callback_public_read(handle, kind, second, value)! match kind { 'log' { handle.option_set_logcb(unsafe { nil }, second)! } 'download' { handle.option_set_dlcb(unsafe { nil }, second)! } 'fetch' { handle.option_set_fetchcb(unsafe { nil }, second)! } 'event' { handle.option_set_eventcb(unsafe { nil }, second)! } 'question' { handle.option_set_questioncb(unsafe { nil }, second)! } else { handle.option_set_progresscb(unsafe { nil }, second)! } } trace << callback_public_read(handle, kind, second, unsafe { nil })! return trace } fn test_public_callback_options_restore_context_and_clear_errno_like_c() ! { root := os.join_path(os.vtmp_dir(), 'alpm-callback-options-${os.getpid()}') os.mkdir_all(root)! defer { os.rmdir_all(root) or { eprintln(err) } } old := initialize('/', root)! defer { old.release() or { eprintln(err) } } public := facade.initialize('/', root)! defer { public.release() or { eprintln(err) } } first := &CallbackState{} second := &CallbackState{} for kind in ['log', 'download', 'fetch', 'event', 'question', 'progress'] { expected := callback_reference_cycle(old, kind, first, second)! actual := callback_public_cycle(public, kind, first, second)! assert actual == expected, kind assert expected == ['${kind}:false:false:0:0', '${kind}:true:true:0:0', '${kind}:true:true:0:0', '${kind}:false:true:0:0'] } assert first.calls.len == 0 assert second.calls == ['log', 'log', 'download', 'download', 'fetch', 'fetch', 'event', 'event', 'question', 'question', 'progress', 'progress'] }