| 1 |
#charset "us-ascii" |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (c) 2000, 2006 Michael J. Roberts |
| 5 |
* |
| 6 |
* This file is part of TADS 3. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <tads.h> |
| 10 |
#include <strbuf.h> |
| 11 |
#include "reflect.h" |
| 12 |
|
| 13 |
/* ------------------------------------------------------------------------ */ |
| 14 |
/* |
| 15 |
* Main reflection services object. |
| 16 |
* |
| 17 |
* During pre-initialization, we'll plug this into the _main module's |
| 18 |
* globals so that the _main module will know it can use reflection |
| 19 |
* services. |
| 20 |
*/ |
| 21 |
reflectionServices: PreinitObject |
| 22 |
/* execute preinitialization */ |
| 23 |
execute() |
| 24 |
{ |
| 25 |
/* plug ourselves into the main globals */ |
| 26 |
mainGlobal.reflectionObj = self; |
| 27 |
|
| 28 |
/* store the main symbol table */ |
| 29 |
symtab_ = t3GetGlobalSymbols(); |
| 30 |
|
| 31 |
/* create a reverse lookup table from the main symbol table */ |
| 32 |
if (symtab_ != nil) |
| 33 |
{ |
| 34 |
/* |
| 35 |
* create a lookup table for the reverse table - it'll be |
| 36 |
* the same size as the original table, so create it using |
| 37 |
* the same statistics |
| 38 |
*/ |
| 39 |
reverseSymtab_ = new LookupTable(symtab_.getBucketCount(), |
| 40 |
symtab_.getEntryCount()); |
| 41 |
|
| 42 |
/* |
| 43 |
* for each entry in the main table, create an entry in the |
| 44 |
* reverse table with the role of key and value reversed - |
| 45 |
* this will allow us to look up any value and find its |
| 46 |
* global symbol, if it has one |
| 47 |
*/ |
| 48 |
symtab_.forEachAssoc({key, val: reverseSymtab_[val] = key}); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/* |
| 53 |
* Convert a value to a symbol, or to a string representation if |
| 54 |
* it's not of a symbolic type. |
| 55 |
*/ |
| 56 |
valToSymbol(val) |
| 57 |
{ |
| 58 |
local sym; |
| 59 |
|
| 60 |
/* the representation depends on the type */ |
| 61 |
switch(dataType(val)) |
| 62 |
{ |
| 63 |
case TypeNil: |
| 64 |
return 'nil'; |
| 65 |
|
| 66 |
case TypeTrue: |
| 67 |
return 'true'; |
| 68 |
|
| 69 |
case TypeInt: |
| 70 |
return toString(val); |
| 71 |
|
| 72 |
case TypeSString: |
| 73 |
case TypeList: |
| 74 |
case TypeObject: |
| 75 |
/* |
| 76 |
* If we're asking about 'self', inherit the handling. Note |
| 77 |
* that, for any object type, x.ofKind(x) is always true, so |
| 78 |
* there's no need for a separate test to see if val equals |
| 79 |
* self. |
| 80 |
*/ |
| 81 |
if (val.ofKind(self)) |
| 82 |
return inherited(); |
| 83 |
|
| 84 |
/* check for intrinsic classes */ |
| 85 |
if (IntrinsicClass.isIntrinsicClass(val)) |
| 86 |
{ |
| 87 |
/* intrinsic classes should always be in the symbol table */ |
| 88 |
sym = reverseSymtab_[val]; |
| 89 |
return (sym != nil ? sym : '{intrinsicClass}'); |
| 90 |
} |
| 91 |
|
| 92 |
/* use our special value-to-symbol method on the object itself */ |
| 93 |
return val.valToSymbol(); |
| 94 |
|
| 95 |
case TypeProp: |
| 96 |
/* |
| 97 |
* this should usually convert to a symbol, but might have |
| 98 |
* been allocated dynamically |
| 99 |
*/ |
| 100 |
sym = reverseSymtab_[val]; |
| 101 |
return (sym != nil ? sym : '{prop}'); |
| 102 |
|
| 103 |
case TypeFuncPtr: |
| 104 |
/* |
| 105 |
* look for a name; if it doesn't have one, it must be an |
| 106 |
* anonymous function |
| 107 |
*/ |
| 108 |
sym = reverseSymtab_[val]; |
| 109 |
return (sym != nil ? sym : '{anonFunc}'); |
| 110 |
|
| 111 |
case TypeEnum: |
| 112 |
/* these should always convert directly to symbols */ |
| 113 |
sym = reverseSymtab_[val]; |
| 114 |
return (sym != nil ? sym : '{enum}'); |
| 115 |
|
| 116 |
case TypeBifPtr: |
| 117 |
/* these should always convert directly to symbols */ |
| 118 |
sym = reverseSymtab_[val]; |
| 119 |
return (sym != nil ? sym : '{intrinsicFunc}'); |
| 120 |
|
| 121 |
case TypeNativeCode: |
| 122 |
return '{native code}'; |
| 123 |
|
| 124 |
default: |
| 125 |
return '???'; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/* |
| 130 |
* Format a stack frame object (of class T3StackInfo). |
| 131 |
*/ |
| 132 |
formatStackFrame(fr, includeSourcePos) |
| 133 |
{ |
| 134 |
local ret = new StringBuffer(); |
| 135 |
|
| 136 |
/* see what kind of frame we have */ |
| 137 |
if (fr.func_ != nil) |
| 138 |
{ |
| 139 |
/* it's a function */ |
| 140 |
ret.append(valToSymbol(fr.func_)); |
| 141 |
} |
| 142 |
else if (fr.obj_ != nil) |
| 143 |
{ |
| 144 |
/* |
| 145 |
* It's an object.property. Check for one special case we |
| 146 |
* want to show specially: if the object is an AnonFuncPtr |
| 147 |
* object, ignore the property and just show it as an |
| 148 |
* anonymous function. |
| 149 |
*/ |
| 150 |
if (fr.obj_.ofKind(AnonFuncPtr)) |
| 151 |
ret.append('{anonFunc}'); |
| 152 |
else |
| 153 |
{ |
| 154 |
ret.append(valToSymbol(fr.self_)); |
| 155 |
ret.append('.'); |
| 156 |
ret.append(valToSymbol(fr.prop_)); |
| 157 |
} |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
/* no function or object - must be a system routine */ |
| 162 |
ret.append('(System)'); |
| 163 |
} |
| 164 |
|
| 165 |
/* if it's not a system routine, add the argument list */ |
| 166 |
if (fr.argList_ != nil) |
| 167 |
{ |
| 168 |
/* add the open paren */ |
| 169 |
ret.append('('); |
| 170 |
|
| 171 |
/* add the arguments */ |
| 172 |
local i, len = fr.argList_.length(); |
| 173 |
for (i = 1 ; i <= len ; ++i) |
| 174 |
{ |
| 175 |
/* if it's not the first one, add a comma */ |
| 176 |
if (i != 1) |
| 177 |
ret.append(', '); |
| 178 |
|
| 179 |
/* add this value */ |
| 180 |
ret.append(valToSymbol(fr.argList_[i]).htmlify()); |
| 181 |
} |
| 182 |
|
| 183 |
/* add any named arguments */ |
| 184 |
if (fr.namedArgs_ != nil) |
| 185 |
{ |
| 186 |
/* add each key from the named argument table */ |
| 187 |
fr.namedArgs_.forEachAssoc(function(key, val) |
| 188 |
{ |
| 189 |
/* add a separator if this isn't the first item */ |
| 190 |
if (i++ != 1) |
| 191 |
ret.append(', '); |
| 192 |
|
| 193 |
/* add this "name: value" */ |
| 194 |
ret.append(key); |
| 195 |
ret.append(':'); |
| 196 |
ret.append(valToSymbol(val)); |
| 197 |
}); |
| 198 |
} |
| 199 |
|
| 200 |
/* add the close paren */ |
| 201 |
ret.append(')'); |
| 202 |
|
| 203 |
/* if desired, add the source location */ |
| 204 |
if (includeSourcePos && fr.srcInfo_ != nil) |
| 205 |
{ |
| 206 |
ret.append(' '); |
| 207 |
ret.append(fr.srcInfo_[1]); |
| 208 |
ret.append(', line '); |
| 209 |
ret.append(fr.srcInfo_[2]); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/* return the result */ |
| 214 |
return toString(ret); |
| 215 |
} |
| 216 |
|
| 217 |
/* the global symbol table */ |
| 218 |
symtab_ = nil |
| 219 |
|
| 220 |
/* the global reverse-lookup symbol table */ |
| 221 |
reverseSymtab_ = nil |
| 222 |
; |
| 223 |
|
| 224 |
/* ------------------------------------------------------------------------ */ |
| 225 |
/* |
| 226 |
* Export the reflection services interfaces used by the VM |
| 227 |
*/ |
| 228 |
export reflectionServices 'reflection.reflectionServices'; |
| 229 |
export valToSymbol 'reflection.valToSymbol'; |
| 230 |
|
| 231 |
/* ------------------------------------------------------------------------ */ |
| 232 |
/* |
| 233 |
* Modify the basic Object class to provide a to-symbol mapping |
| 234 |
*/ |
| 235 |
modify Object |
| 236 |
valToSymbol() |
| 237 |
{ |
| 238 |
/* get my symbol from the global reflection table */ |
| 239 |
local sym = reflectionServices.reverseSymtab_[self]; |
| 240 |
|
| 241 |
/* if we got a symbol, return it */ |
| 242 |
if (sym != nil) |
| 243 |
return sym; |
| 244 |
|
| 245 |
/* |
| 246 |
* We didn't get a symbol, so there's no source file name. See |
| 247 |
* if we can find source-file names for the superclasses, though. |
| 248 |
*/ |
| 249 |
sym = '{obj:'; |
| 250 |
local found = nil; |
| 251 |
foreach (local sc in getSuperclassList()) |
| 252 |
{ |
| 253 |
local scSym; |
| 254 |
|
| 255 |
/* add a comma to the list if this isn't the first element */ |
| 256 |
if (sym != '{obj:') |
| 257 |
sym += ','; |
| 258 |
|
| 259 |
/* if we have a name here, add it to the list */ |
| 260 |
if ((scSym = reflectionServices.reverseSymtab_[sc]) != nil) |
| 261 |
{ |
| 262 |
/* note that we found a named superclass */ |
| 263 |
found = true; |
| 264 |
|
| 265 |
/* add the superclass name to the list */ |
| 266 |
sym += scSym; |
| 267 |
} |
| 268 |
else |
| 269 |
{ |
| 270 |
/* we don't have a name for this superclass; say so */ |
| 271 |
sym += '{anonymous}'; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/* |
| 276 |
* if we found any named superclasses, return the list of names; |
| 277 |
* otherwise, just say (obj) |
| 278 |
*/ |
| 279 |
return (found ? sym + '}' : '{obj}'); |
| 280 |
} |
| 281 |
; |
| 282 |
|
| 283 |
/* ------------------------------------------------------------------------ */ |
| 284 |
/* |
| 285 |
* Modify the String intrinsic class to provide a to-symbol mapping |
| 286 |
*/ |
| 287 |
modify String |
| 288 |
valToSymbol() |
| 289 |
{ |
| 290 |
local ret; |
| 291 |
local i; |
| 292 |
local start; |
| 293 |
|
| 294 |
/* start with an open quote */ |
| 295 |
ret = '\''; |
| 296 |
|
| 297 |
/* loop through the string to find each special character */ |
| 298 |
for (i = 1, local len = length(), start = 1 ; |
| 299 |
i <= len ; ++i) |
| 300 |
{ |
| 301 |
local qu; |
| 302 |
|
| 303 |
/* presume we won't add a quoted character on this round */ |
| 304 |
qu = nil; |
| 305 |
|
| 306 |
/* see what we have here */ |
| 307 |
switch(substr(i, 1)) |
| 308 |
{ |
| 309 |
case '\\': |
| 310 |
qu = '\\\\'; |
| 311 |
break; |
| 312 |
|
| 313 |
case '\'': |
| 314 |
qu = '\\\''; |
| 315 |
break; |
| 316 |
|
| 317 |
case '\n': |
| 318 |
qu = '\\n'; |
| 319 |
break; |
| 320 |
|
| 321 |
case '\t': |
| 322 |
qu = '\\t'; |
| 323 |
break; |
| 324 |
|
| 325 |
case '\b': |
| 326 |
qu = '\\b'; |
| 327 |
break; |
| 328 |
|
| 329 |
case '\ ': |
| 330 |
qu = '\\ '; |
| 331 |
break; |
| 332 |
|
| 333 |
case '\^': |
| 334 |
qu = '\\^'; |
| 335 |
break; |
| 336 |
|
| 337 |
case '\v': |
| 338 |
qu = '\\v'; |
| 339 |
break; |
| 340 |
} |
| 341 |
|
| 342 |
/* |
| 343 |
* if we have a quoted character, add the part up to the |
| 344 |
* quoted character plus the quoted character |
| 345 |
*/ |
| 346 |
if (qu != nil) |
| 347 |
{ |
| 348 |
/* add the part up to here but not including this char */ |
| 349 |
if (i != start) |
| 350 |
ret += substr(start, i - start); |
| 351 |
|
| 352 |
/* add the quoted form of the character */ |
| 353 |
ret += qu; |
| 354 |
|
| 355 |
/* start again after this character */ |
| 356 |
start = i + 1; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
/* add the trailing unquoted part if we haven't already */ |
| 361 |
if (i != start) |
| 362 |
ret += substr(start, i - start); |
| 363 |
|
| 364 |
/* add a close quote and return the result */ |
| 365 |
return ret + '\''; |
| 366 |
} |
| 367 |
; |
| 368 |
|
| 369 |
/* ------------------------------------------------------------------------ */ |
| 370 |
/* |
| 371 |
* Modify the List intrinsic class to provide a to-symbol mapping |
| 372 |
*/ |
| 373 |
modify List |
| 374 |
valToSymbol() |
| 375 |
{ |
| 376 |
local ret; |
| 377 |
|
| 378 |
/* start off with an open bracket */ |
| 379 |
ret = '['; |
| 380 |
|
| 381 |
/* convert each element to symbolic form */ |
| 382 |
for (local i = 1, local len = length() ; i <= len ; ++i) |
| 383 |
{ |
| 384 |
/* add a comma if this isn't the first element */ |
| 385 |
if (i != 1) |
| 386 |
ret += ', '; |
| 387 |
|
| 388 |
/* add this element converted to symbolic form */ |
| 389 |
ret += reflectionServices.valToSymbol(self[i]); |
| 390 |
} |
| 391 |
|
| 392 |
/* add the close bracket and return the result */ |
| 393 |
return ret + ']'; |
| 394 |
} |
| 395 |
; |
| 396 |
|
| 397 |
|
| 398 |
/* ------------------------------------------------------------------------ */ |
| 399 |
/* |
| 400 |
* If desired, modify the BigNumber intrinsic class to provide a |
| 401 |
* to-symbol mapping. We only include this modification if the program |
| 402 |
* is compiled with REFLECT_BIGNUM defined. |
| 403 |
*/ |
| 404 |
#ifdef REFLECT_BIGNUM |
| 405 |
#include "bignum.h" |
| 406 |
|
| 407 |
modify BigNumber |
| 408 |
valToSymbol() |
| 409 |
{ |
| 410 |
/* use the default formatting */ |
| 411 |
return formatString(12); |
| 412 |
} |
| 413 |
; |
| 414 |
|
| 415 |
#endif /* REFLECT_BIGNUM */ |