2 * Acess2 - SpiderScript
3 * - Script Exports (Lang. Namespace)
5 #define _GNU_SOURCE // HACK!
9 #include <spiderscript.h>
12 tSpiderValue *Exports_sizeof(tSpiderScript *Script, int NArgs, tSpiderValue **Args);
13 tSpiderValue *Exports_array(tSpiderScript *Script, int NArgs, tSpiderValue **Args);
14 tSpiderValue *Exports_Lang_Strings_Split(tSpiderScript *Script, int NArgs, tSpiderValue **Args);
15 tSpiderValue *Exports_Lang_Struct(tSpiderScript *Script, int NArgs, tSpiderValue **Args);
18 tSpiderFunction gExports_Lang_Strings_Split = {
20 .Handler = Exports_Lang_Strings_Split,
21 .ReturnType = SS_MAKEARRAY(SS_DATATYPE_STRING),
22 .ArgTypes = {SS_DATATYPE_STRING, SS_DATATYPE_STRING, -1}
24 tSpiderNamespace gExports_NS_Lang_Strings = {
26 .Functions = &gExports_Lang_Strings_Split
29 tSpiderFunction gExports_Lang_Struct = {
31 .Handler = Exports_Lang_Struct,
32 .ReturnType = SS_DATATYPE_OPAQUE,
33 .ArgTypes = {SS_DATATYPE_STRING, -1}
36 tSpiderNamespace gExports_NS_Lang = {
38 .Functions = &gExports_Lang_Struct,
39 .FirstChild = &gExports_NS_Lang_Strings
41 tSpiderNamespace gExportNamespaceRoot = {
42 .FirstChild = &gExports_NS_Lang
45 // -- Global Functions
46 tSpiderFunction gExports_array = {
49 .Handler = Exports_array,
50 .ReturnType = SS_DATATYPE_DYNAMIC,
51 .ArgTypes = {SS_DATATYPE_INTEGER, -1}
53 tSpiderFunction gExports_sizeof = {
54 .Next = &gExports_array,
56 .Handler = Exports_sizeof,
57 .ReturnType = SS_DATATYPE_INTEGER,
58 .ArgTypes = {SS_DATATYPE_UNDEF, -1}
60 tSpiderFunction *gpExports_First = &gExports_sizeof;
63 tSpiderValue *Exports_sizeof(tSpiderScript *Script, int NArgs, tSpiderValue **Args)
65 if(NArgs != 1 || !Args[0]) return NULL;
67 switch( Args[0]->Type )
69 case SS_DATATYPE_STRING:
70 return SpiderScript_CreateInteger(Args[0]->String.Length);
71 case SS_DATATYPE_ARRAY:
72 return SpiderScript_CreateInteger(Args[0]->Array.Length);
78 tSpiderValue *Exports_array(tSpiderScript *Script, int NArgs, tSpiderValue **Args)
80 if(NArgs != 2) return ERRPTR;
81 if(!Args[0] || !Args[1]) return ERRPTR;
83 if(Args[0]->Type != SS_DATATYPE_INTEGER || Args[1]->Type != SS_DATATYPE_INTEGER)
86 int type = Args[1]->Integer;
87 int size = Args[0]->Integer;
89 if( type != SS_DATATYPE_ARRAY )
91 if( !SS_GETARRAYDEPTH(type) ) {
92 // ERROR - This should never happen
95 type = SS_DOWNARRAY(type);
98 return SpiderScript_CreateArray(type, size);
101 tSpiderValue *Exports_Lang_Strings_Split(tSpiderScript *Script, int NArgs, tSpiderValue **Args)
104 void *haystack, *end;
106 tSpiderValue **strings = NULL;
112 if( !Args[0] || !Args[1] )
114 if( Args[0]->Type != SS_DATATYPE_STRING )
116 if( Args[1]->Type != SS_DATATYPE_STRING )
120 len = Args[0]->String.Length;
121 haystack = Args[0]->String.Data;
124 end = memmem(haystack + ofs, len - ofs, Args[1]->String.Data, Args[1]->String.Length);
126 slen = end - (haystack + ofs);
130 strings = realloc(strings, (nSubStrs+1)*sizeof(tSpiderValue*));
131 strings[nSubStrs] = SpiderScript_CreateString(slen, haystack + ofs);
134 ofs += slen + Args[1]->String.Length;
137 // Create output array
138 ret = SpiderScript_CreateArray(SS_DATATYPE_STRING, nSubStrs);
139 memcpy(ret->Array.Items, strings, nSubStrs*sizeof(tSpiderValue*));
145 tSpiderValue *Exports_Lang_Struct(tSpiderScript *Script, int NArgs, tSpiderValue **Args)
148 printf("Exports_Lang_Struct: (Script=%p, NArgs=%i, Args=%p)\n", Script, NArgs, Args);
150 for( i = 0; i < NArgs; i ++ )
152 printf(" Args[%i] = {Type: %i, ", i, Args[i]->Type);
153 switch(Args[i]->Type)
155 case SS_DATATYPE_INTEGER:
156 printf(" Integer: 0x%lx", Args[i]->Integer);
158 case SS_DATATYPE_REAL:
159 printf(" Real: %f", Args[i]->Real);
161 case SS_DATATYPE_STRING:
162 printf(" Length: %i, Data = '%s'", Args[i]->String.Length, Args[i]->String.Data);