| 1 | #charset "us-ascii" |
| 2 | #pragma once |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 2000, 2006 Michael J. Roberts |
| 6 | * |
| 7 | * This file is part of TADS 3. |
| 8 | * |
| 9 | * The header defines the DynamicFunc intrinsic class. This class is used |
| 10 | * for creating new functions from source code strings dynamically at |
| 11 | * run-time. |
| 12 | * |
| 13 | * Programs that use the DynamicFunc class should add the system library |
| 14 | * source file dynfunc.t to the build file list, since it contains some |
| 15 | * additional definitions that are useful with this class. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | /* include our base class definitions */ |
| 20 | #include "systype.h" |
| 21 | |
| 22 | /* |
| 23 | * The DynamicFunc class is used to create new functions from source code |
| 24 | * strings dynamically at run-time. To create a new executable function, |
| 25 | * create a new DynamicFunc object: |
| 26 | * |
| 27 | *. local f = new DynamicFunc('function(x) { return x*2; }', |
| 28 | *. globals, locals, macros); |
| 29 | * |
| 30 | * The first constructor argument is a string giving the source code to |
| 31 | * compile. |
| 32 | * |
| 33 | * The optional second argument is a LookupTable containing the global |
| 34 | * symbols for the compilation. This can be the table returned from |
| 35 | * t3GetGlobalSymbols(), or it can be a custom table. The compiler looks |
| 36 | * up string keys in the table to resolve global symbol names; the |
| 37 | * corresponding values can be objects, properties, enum values, or |
| 38 | * function pointers. |
| 39 | * |
| 40 | * The optional third argument is a StackFrameDesc object, or a list of |
| 41 | * StackFrameDesc objects, giving the stack frames to use for local |
| 42 | * variable access within the dynamic code. The source code can refer to |
| 43 | * local variables in these frames, and can assign new values to them. |
| 44 | * This is analogous to the way anonymous functions can refer to local |
| 45 | * variables in their lexically enclosing scopes. If you supply a list, |
| 46 | * the compiler searches the frames in order of their appearance in the |
| 47 | * list. If a variable appears in more than one frame in the list, the |
| 48 | * compiler will use the first occurrence it finds. |
| 49 | * |
| 50 | * The optional fourth argument is a LookupTable giving the preprocessor |
| 51 | * macros to use for the compilation. This is a table in the format |
| 52 | * returned by t3GetGlobalsSymbols(T3PreprocMacros). |
| 53 | * |
| 54 | * If the 'symtab' or 'macros' arguments are omitted or nil, the |
| 55 | * compilation is done without global symbols or macros, respectively. |
| 56 | * Note that built-in functions and intrinsic classes are identified by |
| 57 | * global symbols, so you won't be able to call any built-ins if you don't |
| 58 | * provide a symbol table. |
| 59 | * |
| 60 | * The source code string can start with the keyword 'function', followed |
| 61 | * by a list of parameter names, followed by the executable statements in |
| 62 | * braces. Or, it can simply be a list of statements, in which case it's a |
| 63 | * function with no arguments, as though it started with 'function()'. |
| 64 | * |
| 65 | * Creating a DynamicFunc compiles the source code. If a compilation error |
| 66 | * occurs (such as a syntax error, or an unknown symbol reference), the |
| 67 | * 'new' call will throw a CompilerException error. The exception object |
| 68 | * contains information on the specific compiler error. |
| 69 | * |
| 70 | * To invoke the new function, simply use the standard function call syntax |
| 71 | * on the object value, as though it were a regular function pointer: |
| 72 | * |
| 73 | *. local y = f(7); |
| 74 | */ |
| 75 | intrinsic class DynamicFunc 'dynamic-func/030000' |
| 76 | { |
| 77 | /* |
| 78 | * Get the original source code of the function. This returns the |
| 79 | * string that was used as the source string to create the function. |
| 80 | */ |
| 81 | getSource(); |
| 82 | } |
| 83 |