From: John Hodge Date: Tue, 17 Apr 2012 15:12:08 +0000 (+0800) Subject: Usermode/SpiderScript - Added language namespace exports X-Git-Tag: rel0.15~714^2~5 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=f12d3444d2efeae97f8a4fc7ecf65b23087a653a;p=tpg%2Facess2.git Usermode/SpiderScript - Added language namespace exports --- diff --git a/Usermode/Libraries/libspiderscript.so_src/ast_to_bytecode.c b/Usermode/Libraries/libspiderscript.so_src/ast_to_bytecode.c index 8d94735b..286869c8 100644 --- a/Usermode/Libraries/libspiderscript.so_src/ast_to_bytecode.c +++ b/Usermode/Libraries/libspiderscript.so_src/ast_to_bytecode.c @@ -152,6 +152,7 @@ int AST_ConvertNode(tAST_BlockInfo *Block, tAST_Node *Node, int bKeepValue) if(ret) return ret; if( blockInfo.StackDepth != 0 ) { AST_RuntimeError(node, "Stack not reset at end of node"); + blockInfo.StackDepth = 0; } } @@ -538,7 +539,7 @@ int AST_ConvertNode(tAST_BlockInfo *Block, tAST_Node *Node, int bKeepValue) // > Type check ret = _StackPop(Block, Node, SS_DATATYPE_UNDEF); if(ret < 0) return -1; - if(ret != SS_DATATYPE_ARRAY && (ret >> 16) == 0) { + if(ret != SS_DATATYPE_ARRAY && SS_GETARRAYDEPTH(ret) == 0) { AST_RuntimeError(Node, "Type mismatch, Expected an array, got %i", ret); return -2; @@ -555,7 +556,7 @@ int AST_ConvertNode(tAST_BlockInfo *Block, tAST_Node *Node, int bKeepValue) // Update the array depth if( i != SS_DATATYPE_ARRAY ) { - i -= 0x10000; // Decrease the array level + i = SS_DOWNARRAY(i); // Decrease the array level } ret = _StackPush(Block, Node, i); if(ret < 0) return -1; @@ -664,12 +665,13 @@ int AST_ConvertNode(tAST_BlockInfo *Block, tAST_Node *Node, int bKeepValue) int BC_SaveValue(tAST_BlockInfo *Block, tAST_Node *DestNode) { - int ret; + int ret, type; switch(DestNode->Type) { // Variable, simple case NODETYPE_VARIABLE: ret = BC_Variable_SetValue( Block, DestNode ); + if(ret) return ret; break; // Array index case NODETYPE_INDEX: @@ -677,11 +679,12 @@ int BC_SaveValue(tAST_BlockInfo *Block, tAST_Node *DestNode) if(ret) return ret; ret = _StackPop(Block, DestNode->BinOp.Left, SS_DATATYPE_UNDEF); if(ret < 0) return -1; - if(ret != SS_DATATYPE_ARRAY && (ret >> 16) == 0) { + if(ret != SS_DATATYPE_ARRAY && SS_GETARRAYDEPTH(ret) == 0) { AST_RuntimeError(DestNode, "Type mismatch, Expected an array, got %i", ret); return -2; } + type = SS_DOWNARRAY(ret); ret = AST_ConvertNode(Block, DestNode->BinOp.Right, 1); // Offset if(ret) return ret; @@ -689,6 +692,7 @@ int BC_SaveValue(tAST_BlockInfo *Block, tAST_Node *DestNode) if(ret < 0) return -1; Bytecode_AppendSetIndex( Block->Handle ); + _StackPop(Block, DestNode, type); break; // Object element case NODETYPE_ELEMENT: diff --git a/Usermode/Libraries/libspiderscript.so_src/exec.c b/Usermode/Libraries/libspiderscript.so_src/exec.c index f0bc6975..c7fed035 100644 --- a/Usermode/Libraries/libspiderscript.so_src/exec.c +++ b/Usermode/Libraries/libspiderscript.so_src/exec.c @@ -16,6 +16,7 @@ // === IMPORTS === extern tSpiderFunction *gpExports_First; +extern tSpiderNamespace gExportNamespaceRoot; extern tSpiderValue *AST_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, int NArguments, tSpiderValue **Arguments); extern tSpiderValue *Bytecode_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, int NArguments, tSpiderValue **Args); @@ -63,11 +64,12 @@ void *SpiderScript_int_GetNamespace(tSpiderScript *Script, tSpiderNamespace *Roo // Check for this level for( ns = lastns->FirstChild; ns; ns = ns->Next ) { + printf("%p %.*s == %s\n", lastns, len, name, ns->Name); if( strncmp(name, ns->Name, len) == 0 && ns->Name[len] == 0 ) break ; } - if(!ns) return NULL; + if(!ns) return NULL; if(!end && !bTriedBase) { end = ItemPath - 1; // -1 to counter (name = end + 1) @@ -145,10 +147,12 @@ tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script, for( i = 0; i == 0 || (DefaultNamespaces && DefaultNamespaces[i-1]); i ++ ) { const char *ns = DefaultNamespaces ? DefaultNamespaces[i] : NULL; - fcn = SpiderScript_int_GetNativeFunction(Script, &Script->Variant->RootNamespace, - ns, Function); + fcn = SpiderScript_int_GetNativeFunction(Script, &Script->Variant->RootNamespace, ns, Function); if( fcn ) break; - + + fcn = SpiderScript_int_GetNativeFunction(Script, &gExportNamespaceRoot, ns, Function); + if( fcn ) break; + // TODO: Script namespacing } @@ -297,9 +301,12 @@ tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script, // Scan list, Last item should always be NULL, so abuse that to check non-prefixed for( i = 0; i == 0 || DefaultNamespaces[i-1]; i ++ ) { - class = SpiderScript_int_GetNativeClass(Script, &Script->Variant->RootNamespace, - DefaultNamespaces[i], ClassPath); - if( class != NULL ) break; + const char *ns = DefaultNamespaces[i]; + class = SpiderScript_int_GetNativeClass(Script, &Script->Variant->RootNamespace, ns, ClassPath); + if( class ) break; + + class = SpiderScript_int_GetNativeClass(Script, &gExportNamespaceRoot, ns, ClassPath); + if( class ) break; // TODO: Language defined classes } diff --git a/Usermode/Libraries/libspiderscript.so_src/exec_bytecode.c b/Usermode/Libraries/libspiderscript.so_src/exec_bytecode.c index 6817c243..82444b8d 100644 --- a/Usermode/Libraries/libspiderscript.so_src/exec_bytecode.c +++ b/Usermode/Libraries/libspiderscript.so_src/exec_bytecode.c @@ -403,11 +403,13 @@ int Bytecode_int_LocalBinOp_Real(int Operation, tBC_StackEnt *Val1, tBC_StackEnt { switch(Operation) { + // Real = Real OP Real case BC_OP_ADD: Val1->Real = Val1->Real + Val2->Real; return 0; case BC_OP_SUBTRACT: Val1->Real = Val1->Real - Val2->Real; return 0; case BC_OP_MULTIPLY: Val1->Real = Val1->Real * Val2->Real; return 0; case BC_OP_DIVIDE: Val1->Real = Val1->Real / Val2->Real; return 0; + // Bool/Integer = Real OP Real case BC_OP_EQUALS: Val1->Integer = (Val1->Real == Val2->Real); break; case BC_OP_NOTEQUALS: Val1->Integer = (Val1->Real != Val2->Real); break; case BC_OP_LESSTHAN: Val1->Integer = (Val1->Real < Val2->Real); break; @@ -596,25 +598,20 @@ int Bytecode_int_ExecuteFunction(tSpiderScript *Script, tScript_Function *Fcn, t PUT_STACKVAL(val1); break; } - switch(val2.Type * 100 + val1.Type ) - { - case SS_DATATYPE_INTEGER*100 + SS_DATATYPE_REAL: + if( val2.Type == SS_DATATYPE_INTEGER && val1.Type == SS_DATATYPE_REAL ) { val2.Integer = val1.Real; - PUT_STACKVAL(val2); - break; - case SS_DATATYPE_REAL*100 + SS_DATATYPE_INTEGER: + } + else if( val2.Type == SS_DATATYPE_REAL && val2.Type == SS_DATATYPE_INTEGER ) { val2.Real = val1.Integer; - PUT_STACKVAL(val2); - break; - default: { + } + else { pval1 = Bytecode_int_GetSpiderValue(&val1, &tmpVal1); pval2 = SpiderScript_CastValueTo(val2.Type, pval1); if(pval1 != &tmpVal1) SpiderScript_DereferenceValue(pval1); Bytecode_int_SetSpiderValue(&val2, pval2); SpiderScript_DereferenceValue(pval2); - PUT_STACKVAL(val2); - } break; } + PUT_STACKVAL(val2); break; case BC_OP_DUPSTACK: diff --git a/Usermode/Libraries/libspiderscript.so_src/exports.c b/Usermode/Libraries/libspiderscript.so_src/exports.c index 4c73eb9f..b114c9ce 100644 --- a/Usermode/Libraries/libspiderscript.so_src/exports.c +++ b/Usermode/Libraries/libspiderscript.so_src/exports.c @@ -8,13 +8,68 @@ #include // === PROTOTYPES === +tSpiderValue *Exports_sizeof(tSpiderScript *Script, int NArgs, tSpiderValue **Args); +tSpiderValue *Exports_Lang_Strings_Split(tSpiderScript *Script, int NArgs, tSpiderValue **Args); tSpiderValue *Exports_Lang_Struct(tSpiderScript *Script, int NArgs, tSpiderValue **Args); // === GLOBALS === -tSpiderFunction gExports_Lang_Struct = {NULL,"Lang.Struct", Exports_Lang_Struct, {SS_DATATYPE_STRING,-1}}; -tSpiderFunction *gpExports_First = &gExports_Lang_Struct; +tSpiderFunction gExports_Lang_Strings_Split = { + .Name = "Split", + .Handler = Exports_Lang_Strings_Split, + .ReturnType = SS_MAKEARRAY(SS_DATATYPE_STRING), + .ArgTypes = {SS_DATATYPE_STRING, SS_DATATYPE_STRING, -1} +}; +tSpiderNamespace gExports_NS_Lang_Strings = { + .Name = "Strings", + .Functions = &gExports_Lang_Strings_Split + }; + +tSpiderFunction gExports_Lang_Struct = { + .Name = "Struct", + .Handler = Exports_Lang_Struct, + .ReturnType = SS_DATATYPE_OPAQUE, + .ArgTypes = {SS_DATATYPE_STRING, -1} +}; +// - Lang Namespace +tSpiderNamespace gExports_NS_Lang = { + .Name = "Lang", + .Functions = &gExports_Lang_Struct, + .FirstChild = &gExports_NS_Lang_Strings + }; +tSpiderNamespace gExportNamespaceRoot = { + .FirstChild = &gExports_NS_Lang +}; + +// -- Global Functions +tSpiderFunction gExports_sizeof = { + .Name = "sizeof", + .Handler = Exports_sizeof, + .ReturnType = SS_DATATYPE_INTEGER, + .ArgTypes = {SS_DATATYPE_UNDEF, -1} +}; +tSpiderFunction *gpExports_First; // === CODE === +tSpiderValue *Exports_sizeof(tSpiderScript *Script, int NArgs, tSpiderValue **Args) +{ + if(NArgs != 1 || !Args[0]) return NULL; + + switch( Args[0]->Type ) + { + case SS_DATATYPE_STRING: + return SpiderScript_CreateInteger(Args[0]->String.Length); + case SS_DATATYPE_ARRAY: + return SpiderScript_CreateInteger(Args[0]->Array.Length); + default: + return NULL; + } +} + +tSpiderValue *Exports_Lang_Strings_Split(tSpiderScript *Script, int NArgs, tSpiderValue **Args) +{ + return NULL; +} + tSpiderValue *Exports_Lang_Struct(tSpiderScript *Script, int NArgs, tSpiderValue **Args) { int i; diff --git a/Usermode/include/spiderscript.h b/Usermode/include/spiderscript.h index 56a48e65..e0497305 100644 --- a/Usermode/include/spiderscript.h +++ b/Usermode/include/spiderscript.h @@ -65,6 +65,10 @@ enum eSpiderScript_DataTypes NUM_SS_DATATYPES }; +#define SS_MAKEARRAY(_type) ((_type) + 0x10000) +#define SS_DOWNARRAY(_type) ((_type) - 0x10000) +#define SS_GETARRAYDEPTH(_type) ((_type) >> 16) + enum eSpiderValueOps { SS_VALUEOP_NOP, @@ -236,6 +240,12 @@ struct sSpiderFunction * \brief Function handler */ tSpiderValue *(*Handler)(tSpiderScript *Script, int nParams, tSpiderValue **Parameters); + + /** + * \brief What type is returned + */ + int ReturnType; + /** * \brief Argument types *