Executing Generated Code

Macros vs. Declarations

CloudScript has the capability to execute code that is generated at runtime.  There are two kinds of such code:

Execute Locally vs. Execute in new WorkSpace

Code may be executed in the same workspace as the "parent" program or may be executed in a new workspace.  Code executed in a separate (child) workspace has no effect on the parent workspace.  Any text generated in the child workspace's Client object must be retrieved with the WorkSpace GetText member function. 

Executing a Macro

To execute a macro (inline code, no declarations), use the any RunMacro (string code) function call.

RunMacro ("Client << \"Hello, world.<br>\");

The RunMacro function returns the value of a return statement, if any, or null;

Runtime Code

Code may be produced at runtime typically by reading it in from a file.  You could also generate code into strings using your own algorithms.

Once you have the code in a string, you can declare it using the Decl (string code) function.  Remember, the code may contain only declarations, i.e., function, variable, class declarations.

Once declared, you may execute a function simply by calling it.

Decl ("f(x){return x*x;");
y = f(5);  // y = 25

You may also invoke the Eval (string expression) function.

Decl ("f(x){return x*x;");
y = Eval("f(5)");  // y = 25

Typically, code is read in from a file.  Use the GetFile (string filename) to read the file into a string.  Thus, assuming the file f.cw contains

f(x){return x*x;)

Then

Decl (GetFile("f.cw"));
y = Eval("f(5)");  // y = 25

will result in assigning the value 25 to the variable y.

New Workspace

To create a new WorkSpace, use the WorkSpace constructor.  Member functions of a WorkSpace object are

Thus, assuming the file somecode.cw contains:

main()
{
Client << "Hello, world.";
}

 

ws = new WorkSpace;
ws.Decl (GetFile("somecode.cw"));
ws.Eval ("main()");
Client << ws.GetText();

Note:  the return value from an Eval in an alternate WorkSpace is lost.  Only the string value of the Client object may be accessed.

Query Strings

A workspace can be "seeded" with a query string.  A query string is an URL-encoded string with name/value pairs separated by ampersands (the stuff after the "?" in an URL.  To set the Query String, use the workspace's SetQueryString (string querystring) function.

ws.SetQueryString("LastName=Smith&FirstName=Bob&age=44");

will have the same effect on the WorkSpace as if it had been called directly by a Client with the given query string.  I.e., global variables will be set up with the same names as the variable names in the query string.  Also, the table, _QueryValues will be filled with the name/value pairs.

Summary

The Decl and Execute functions are global functions for declaring and executing code.  The Evaluate global function returns the value of an expression.

The WorkSpace object has a Decl member function and an Execute member function.  The string value of the Client object in the WorkSpace may  be fetched using the GetText member function of the WorkSpace object.