Function xsCreateMachine
Returns a machine if successful, otherwise null
.
xs .bindings .xsMachineRecord* xsCreateMachine
(
const(xs .bindings .xsCreationRecord*) creation,
string name,
void* context = null
);
xs .bindings .xsMachineRecord* xsCreateMachine
(
const(xs .bindings .xsCreationRecord*) creation,
const(char*) name,
void* context = null
);
Regarding the parameters of the machine that are specified in the xsCreation
structure:
- A machine manages strings and bytecodes in chunks. The initial chunk size is the initial size of the memory allocated to chunks. The incremental chunk size tells the runtime how to expand the memory allocated to chunks.
- A machine uses a heap and a stack of slots. The initial heap count is the initial number of slots allocated to the heap. The incremental heap count tells the runtime how to increase the number of slots allocated to the heap. The stack count is the number of slots allocated to the stack.
- The symbol count is the number of symbols the machine will use. The symbol modulo is the size of the hash table the machine will use for symbols. A symbol binds a string value and an identifier; see
xsID
.
Parameters
Name | Description |
---|---|
creation | The parameters of the machine |
name | The name of the machine as a string |
context | The initial context of the machine, or null |
Examples
The following example illustrates the use of xsCreateMachine
and xsDeleteMachine
.
int main(int argc, char* argv[]) {
typedef struct {
int argc;
char** argv;
} xsContext;
void xsMainContext(xsMachine* theMachine, int argc, char* argv[])
{
xsContext* aContext;
aContext = malloc(sizeof(xsContext));
if (aContext) {
aContext->argc = argc;
aContext->argv = argv;
xsSetContext(theMachine, aContext);
xsSetContext(theMachine, NULL);
free(aContext);
}
else
fprintf(stderr, "### Cannot allocate context\n");
}
xsCreation aCreation = {
128 * 1024 * 1024, /* initialChunkSize */
16 * 1024 * 1024, /* incrementalChunkSize */
4 * 1024 * 1024, /* initialHeapCount */
1 * 1024 * 1024, /* incrementalHeapCount */
1024, /* stack count */
2048+1024, /* key count */
1993, /* name modulo */
127 /* symbol modulo */
};
xsMachine* aMachine;
aMachine = xsCreateMachine(&aCreation, "machine", NULL);
if (aMachine) {
xsMainContext(aMachine, argc, argv);
xsDeleteMachine(aMachine);
}
else
fprintf(stderr, "### Cannot allocate machine\n");
return 0;
}