2 source [
file join [
file dirname [
info script]] tobj.tcl]
4 namespace eval DataStore {
5 variable _store_template {
8 if {![info exists _ctx]} { set _ctx [tdict create] }
12 set _ctx [tdict create]
15 proc GetFullDataStore {} {
21 proc Load {tDictNode} {
22 if {![tobj isobj $tDictNode] || [tobj type $tDictNode] ne "Dict"} {
23 error "@DS@::Load: expected Dict tobj" "" {CTX_INVALID_ARGS}
30 if {[llength $args] < 2} { error "@DS@::SetStr: too few args" "" {CTX_INVALID_ARGS} }
32 tdict set _ctx {*}[lrange $args 0 end-1] [tobj String [lindex $args end]]
36 if {[llength $args] < 2} { error "@DS@::SetNum: too few args" "" {CTX_INVALID_ARGS} }
38 tdict set _ctx {*}[lrange $args 0 end-1] [tobj Number [lindex $args end]]
42 if {[llength $args] < 2} { error "@DS@::SetBool: too few args" "" {CTX_INVALID_ARGS} }
44 tdict set _ctx {*}[lrange $args 0 end-1] [tobj Bool [lindex $args end]]
48 if {[llength $args] < 1} { error "@DS@::SetNull: too few args" "" {CTX_INVALID_ARGS} }
50 tdict set _ctx {*}$args [tobj Null]
54 if {[llength $args] < 2} { error "@DS@::Set: too few args" "" {CTX_INVALID_ARGS} }
56 tdict set _ctx {*}[lrange $args 0 end-1] [lindex $args end]
60 if {[llength $args] < 2} { error "@DS@::Lappend: too few args" "" {CTX_INVALID_ARGS} }
62 set tl [tdict get $_ctx {*}[lrange $args 0 end-1]]
63 if {![tobj isobj $tl] || [tobj type $tl] ne "List"} {
64 error "@DS@::Lappend: not a List node at path" "" {CTX_NOT_A_LIST}
67 if {![tobj isobj [lindex $args end]]} {
68 error "@DS@::Lappend: expected tobj for value" "" {CTX_INVALID_ARGS}
71 tlist append tl [lindex $args end]
72 tdict set _ctx {*}[lrange $args 0 end-1] $tl
76 if {[llength $args] < 1} { error "@DS@::GetObj: too few args" "" {CTX_INVALID_ARGS} }
78 if {![tdict exists $_ctx {*}$args]} { error "@DS@::GetObj: key not found: $args" "" {CTX_NOT_FOUND} }
79 return [tdict get $_ctx {*}$args]
82 proc GetObjOr {args} {
83 if {[llength $args] < 2} { error "@DS@::GetObjOr: too few args" "" {CTX_INVALID_ARGS} }
85 set keys [lrange $args 0 end-1]
86 set defaultObj [lindex $args end]
87 if {[tdict exists $_ctx {*}$keys]} {
88 return [tdict get $_ctx {*}$keys]
94 if {[llength $args] < 1} { error "@DS@::Get: too few args" "" {CTX_INVALID_ARGS} }
96 if {![tdict exists $_ctx {*}$args]} { error "@DS@::Get: key not found: $args" "" {CTX_NOT_FOUND} }
97 set _node [tdict get $_ctx {*}$args]
98 set _type [tobj type $_node]
99 if {$_type eq "Dict" || $_type eq "List"} { return $_node }
100 return [tobj value $_node]
104 if {[llength $args] < 2} { error "@DS@::GetOr: too few args" "" {CTX_INVALID_ARGS} }
106 set keys [lrange $args 0 end-1]
107 set defaultObj [lindex $args end]
108 if {[tdict exists $_ctx {*}$keys]} {
109 set _node [tdict get $_ctx {*}$keys]
110 set _type [tobj type $_node]
111 if {$_type eq "Dict" || $_type eq "List"} { return $_node }
112 return [tobj value $_node]
118 if {[llength $args] < 1} { error "@DS@::Exists: too few args" "" {CTX_INVALID_ARGS} }
120 return [tdict exists $_ctx {*}$args]
124 if {[llength $args] < 1} { error "@DS@::Remove: too few args" "" {CTX_INVALID_ARGS} }
126 if {![tdict exists $_ctx {*}$args]} { return 0 }
127 set _ctx [tdict remove $_ctx {*}$args]
133 return [tdict size $_ctx]
138 if {[llength $args] == 0} { return [tdict keys $_ctx] }
139 if {![tdict exists $_ctx {*}$args]} { return {} }
140 set sub [tdict get $_ctx {*}$args]
141 if {![tobj isobj $sub] || [tobj type $sub] ne "Dict"} {
142 error "@DS@::Keys: not a Dict node at path" "" {CTX_NOT_A_DICT}
144 return [dict keys [tobj value $sub]]
147 proc ToJson {{flag {}}} {
149 if {$flag eq "-pretty"} { return [tobj tojson $_ctx -pretty] }
150 return [tobj tojson $_ctx]
153 proc SaveJsonToFile {filename {overwrite 0}} {
155 set json [ToJson -pretty]
156 if {[file exists $filename] && !$overwrite} {
157 error "@DS@::SaveJsonToFile: file exists" "" {CTX_FILE_EXISTS}
159 set fh [open $filename w]
164 proc SaveToFile {filename {overwrite 0}} {
166 if {[file exists $filename] && !$overwrite} {
167 error "@DS@::SaveToFile: file exists" "" {CTX_FILE_EXISTS}
169 set fh [open $filename w]
174 proc LoadFromFile {filename} {
175 if {![file exists $filename]} {
176 error "@DS@::LoadFromFile: file not found" "" {CTX_FILE_NOT_FOUND}
178 set fh [open $filename r]
179 set data [string trim [read $fh]]
181 if {![tobj isobj $data] || [tobj type $data] ne "Dict"} {
182 error "@DS@::LoadFromFile: invalid context data" "" {CTX_INVALID_DATA}
195 variable _stores [list]
197 variable _store_template
199 if {[
namespace exists $name]} {
200 error "DataStore::create: namespace '$name' already exists" "" {NS_ALREADY_EXISTS}
203 lappend _stores $name
204 set ds [
string map [list @DS@ $name] $_store_template]
210 set root [tdict create]
211 foreach name $_stores {
212 if {[
namespace exists ::${name}]} {
213 tdict set root $name [${name}::GetFullDataStore]
219 proc Deserialize {tDictRoot} {
220 if {![tobj isobj $tDictRoot] || [tobj type $tDictRoot] ne "Dict"} {
221 error "DataStore::Deserialize: expected Dict tobj" "" {CTX_INVALID_ARGS}
223 foreach name [tdict keys $tDictRoot] {
224 puts "Deserializing context store '$name'"
225 if {![
namespace exists ::${name}]} {
create $name}
226 ${name}::Load [tdict get $tDictRoot $name]
232 proc _OnError {cmd } {
235 set exitCode [
expr {[llength $cmd] > 1 ? [lindex $cmd 1] : 0}]
236 set exitInfo [tdict create]
237 tdict set exitInfo code [tobj Number $exitCode]
238 if {$exitCode != 0} {
239 tdict set exitInfo error_info [tobj String [
expr {[info exists ::errorInfo] ? $::errorInfo : ""}]]
240 tdict set exitInfo error_code [tobj String [
expr {[info exists ::errorCode] ? $::errorCode : ""}]]
243 set locs [dict create]
244 for {
set i 1} {$i <= [
info frame]} {
incr i} {
245 set f [
info frame $i]
246 if {[dict exists $f level] && [dict exists $f file]} {
247 dict set locs [dict get $f level] "[
file tail [dict get $f file]]:[dict get $f line]"
250 set stack_frames [list]
251 for {
set i 1} {$i <= [
info level]} {
incr i} {
252 set loc [
expr {[dict exists $locs $i] ? [dict get $locs $i] : "?"}]
253 lappend stack_frames "[
info level $i] ($loc)"
255 tdict set exitInfo stack_trace [tlist create {*}$stack_frames]
257 tdict set root _exit $exitInfo
259 set json [tobj tojson $root -pretty]
260 set filename [
file join [Repo::GetOr repo_path .] .hog_context_error.json]
261 set fh [open $filename w]
266 proc _OnExit {cmd op} {
269 set exitCode [
expr {[llength $cmd] > 1 ? [lindex $cmd 1] : 0}]
270 set exitInfo [tdict create]
271 tdict set exitInfo code [tobj Number $exitCode]
272 if {$exitCode != 0} {
273 tdict set exitInfo error_info [tobj String [
expr {[info exists ::errorInfo] ? $::errorInfo : ""}]]
274 tdict set exitInfo error_code [tobj String [
expr {[info exists ::errorCode] ? $::errorCode : ""}]]
276 tdict set root _exit $exitInfo
278 set json [tobj tojson $root -pretty]
279 if {$DataStore::inIDE} {
280 set filename [
file join [Repo::GetOr repo_path .] .hog_context_ide_exit.json]
282 set filename [
file join [Repo::GetOr repo_path .] .hog_context_launcher_exit.json]
284 set fh [open $filename w]
290 trace add execution exit enter DataStore::_OnExit