NAME
std/internals - Runtime internals helpers for advanced modules.
SYNOPSIS
from std/internals import class_name, classof, object_slots, ansi_esc,
ref_id, to_String, to_Number, to_Boolean, to_Regexp,
to_Regexp_with_flags,
make_instance, setprop, getprop, load_module;
let obj_class := class_name(some_object);
let obj_class_value := classof(some_object);
let slots := object_slots(some_object);
let id := ref_id(some_object);
IMPLEMENTATION SUPPORT
This module is supported by all implementations of ZuzuScript.
DESCRIPTION
This module provides low-level reflection helpers used by advanced pure-Zuzu modules (for example std/dump). These functions are not intended for general application code.
Special properties are lexical metadata, not symbol bindings. Calling setprop("paths", SomeClass) does not create an identifier named paths.
EXPORTS
Functions
class_name(value)Parameters:
valueis any value. Returns:Stringornull. Returns the class name for object instances.classof(value)Parameters:
valueis any value. Returns: class value ornull. Returns the class value for object and collection values.object_slots(value)Parameters:
valueis any value. Returns:Dictornull. Returns a dictionary of object slots, excluding private underscore-prefixed slots.ansi_esc()Parameters: none. Returns:
String. Returns a one-character ANSI escape string.ref_id(value)Parameters:
valueis any value. Returns:Stringornull. Returns a stable reference id for reference-like values to support cycle detection.to_String(value)Parameters:
valueis any value. Returns:String. Coercesvalueusing the same rules as string operators.to_Number(value)Parameters:
valueis any value. Returns:Number. Coercesvalueusing the same rules as numeric operators.to_Boolean(value)Parameters:
valueis any value. Returns:Boolean. Coercesvalueusing the same rules as logical operators and conditions.to_Regexp(value)Parameters:
valueis a regexp or pattern-like value. Returns:Regexp. Returnsvalueunchanged when it is a regexp, otherwise coerces it to a string and compiles it as a regexp.to_Regexp_with_flags(value, flags)Parameters:
valueis a regexp or pattern-like value andflagsis a string of regexp flags. Returns:Regexp. Coercesvalueto a pattern and compiles it usingflags.make_instance(klass, dict := null)Parameters:
klassis a class value anddictis an optional slot dictionary. Returns: object. Creates an instance without calling__build__.load_module(module, symbol := null)Parameters:
moduleis a module name andsymbolis an optional export name. Returns:Dictor value. Loads a module and returns all exports, or one named export.setprop(key, value)Parameters:
keyis a string andvalueis any value. Returns:null. Sets a lexically scoped runtime special property on the current frame.getprop(key)Parameters:
keyis a string. Returns: value ornull. Returns a lexically scoped runtime special property via outward lexical lookup.
EXAMPLES
from std/internals import setprop, getprop;
from std/path/z import ZPath;
setprop( "paths", ZPath );
say( getprop( "paths" ) ); // Class object
do {
setprop( "paths", null );
say( getprop( "paths" ) ); // null (shadowed in this block)
};
say( getprop( "paths" ) ); // ZPath (outer lexical value)
COPYRIGHT AND LICENCE
std/internals is copyright Toby Inkster.
It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.