1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00

Back port changes to squirrel library. Separate mdoule speciffic changes and/or additions to squirrel into their own files.

This commit is contained in:
Sandu Liviu Catalin 2019-04-24 22:33:42 +03:00
parent 21a00e20b7
commit 1f27146e6c
28 changed files with 254 additions and 43 deletions

View File

@ -190,6 +190,7 @@
<Unit filename="../external/Squirrel/Lib/sqstdstring.cpp" />
<Unit filename="../external/Squirrel/Lib/sqstdsystem.cpp" />
<Unit filename="../external/Squirrel/sqapi.cpp" />
<Unit filename="../external/Squirrel/sqapiex.cpp" />
<Unit filename="../external/Squirrel/sqarray.h" />
<Unit filename="../external/Squirrel/sqbaselib.cpp" />
<Unit filename="../external/Squirrel/sqclass.cpp" />
@ -221,6 +222,7 @@
<code_completion />
<envvars />
<debugger />
<fortran_project />
</Extensions>
</Project>
</CodeBlocks_project_file>

View File

@ -1,7 +1,9 @@
/* see copyright notice in squirrel.h */
#include <squirrel.h>
#include <sqstdaux.h>
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
void sqstd_printcallstack(HSQUIRRELVM v)
{
@ -128,3 +130,22 @@ void sqstd_seterrorhandlers(HSQUIRRELVM v)
sq_newclosure(v,_sqstd_aux_printerror,0);
sq_seterrorhandler(v);
}
SQRESULT sqstd_throwerrorf(HSQUIRRELVM v,const SQChar *err,...)
{
SQInteger n=256;
va_list args;
begin:
va_start(args,err);
SQChar *b=sq_getscratchpad(v,n);
SQInteger r=scvsprintf(b,n,err,args);
va_end(args);
if (r>=n) {
n=r+1;//required+null
goto begin;
} else if (r<0) {
return sq_throwerror(v,_SC("@failed to generate formatted error message"));
} else {
return sq_throwerror(v,b);
}
}

View File

@ -6,6 +6,7 @@
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <stdarg.h>
#define MAX_FORMAT_LEN 20
#define MAX_WFORMAT_LEN 3
@ -153,6 +154,25 @@ SQRESULT sqstd_format(HSQUIRRELVM v,SQInteger nformatstringidx,SQInteger *outlen
return SQ_OK;
}
void sqstd_pushstringf(HSQUIRRELVM v,const SQChar *s,...)
{
SQInteger n=256;
va_list args;
begin:
va_start(args,s);
SQChar *b=sq_getscratchpad(v,n);
SQInteger r=scvsprintf(b,n,s,args);
va_end(args);
if (r>=n) {
n=r+1;//required+null
goto begin;
} else if (r<0) {
sq_pushnull(v);
} else {
sq_pushstring(v,b,r);
}
}
static SQInteger _string_printf(HSQUIRRELVM v)
{
SQChar *dest = NULL;

View File

@ -424,17 +424,6 @@ SQRESULT sq_setnativeclosurename(HSQUIRRELVM v,SQInteger idx,const SQChar *name)
}
return sq_throwerror(v,_SC("the object is not a nativeclosure"));
}
SQRESULT sq_getnativeclosurepointer(HSQUIRRELVM v,SQInteger idx,SQFUNCTION *f)
{
SQObject o = stack_get(v, idx);
if(sq_type(o) == OT_NATIVECLOSURE)
{
SQNativeClosure *c = _nativeclosure(o);
if (f) *f = c->_function;
return SQ_OK;
}
return sq_throwerror(v,_SC("the object is not a native closure"));
}
SQRESULT sq_setparamscheck(HSQUIRRELVM v,SQInteger nparamscheck,const SQChar *typemask)
{

68
external/Squirrel/sqapiex.cpp vendored Normal file
View File

@ -0,0 +1,68 @@
/*
see copyright notice in squirrel.h
*/
#include "sqpcheader.h"
#include "sqvm.h"
#include "sqstring.h"
#include "sqtable.h"
#include "sqarray.h"
#include "sqfuncproto.h"
#include "sqclosure.h"
#include "squserdata.h"
#include "sqcompiler.h"
#include "sqfuncstate.h"
#include "sqclass.h"
#include <squirrelex.h>
#include <stdarg.h>
SQRESULT sq_throwerrorf(HSQUIRRELVM v,const SQChar *err,...)
{
SQInteger n=256;
va_list args;
begin:
va_start(args,err);
SQChar *b=sq_getscratchpad(v,n);
SQInteger r=scvsprintf(b,n,err,args);
va_end(args);
if (r>=n) {
n=r+1;//required+null
goto begin;
} else if (r<0) {
v->_lasterror=SQString::Create(_ss(v),_SC("@failed to generate formatted error message"));
} else {
v->_lasterror=SQString::Create(_ss(v),b,r);
}
return SQ_ERROR;
}
void sqstd_pushstringf(HSQUIRRELVM v,const SQChar *s,...)
{
SQInteger n=256;
va_list args;
begin:
va_start(args,s);
SQChar *b=sq_getscratchpad(v,n);
SQInteger r=scvsprintf(b,n,s,args);
va_end(args);
if (r>=n) {
n=r+1;//required+null
goto begin;
} else if (r<0) {
v->PushNull();
} else {
v->Push(SQObjectPtr(SQString::Create(_ss(v),b,r)));
}
}
SQRESULT sq_getnativeclosurepointer(HSQUIRRELVM v,SQInteger idx,SQFUNCTION *f)
{
SQObject o = stack_get(v, idx);
if(sq_type(o) == OT_NATIVECLOSURE)
{
SQNativeClosure *c = _nativeclosure(o);
if (f) *f = c->_function;
return SQ_OK;
}
return sq_throwerror(v,_SC("the object is not a native closure"));
}

View File

@ -499,6 +499,30 @@ static SQInteger table_filter(HSQUIRRELVM v)
return 1;
}
#define TABLE_TO_ARRAY_FUNC(_funcname_,_valname_) static SQInteger _funcname_(HSQUIRRELVM v) \
{ \
SQObject &o = stack_get(v, 1); \
SQTable *t = _table(o); \
SQObjectPtr itr, key, val; \
SQObjectPtr _null; \
SQInteger nitr, n = 0; \
SQInteger nitems = t->CountUsed(); \
SQArray *a = SQArray::Create(_ss(v), nitems); \
a->Resize(nitems, _null); \
if (nitems) { \
while ((nitr = t->Next(false, itr, key, val)) != -1) { \
itr = (SQInteger)nitr; \
a->Set(n, _valname_); \
n++; \
} \
} \
v->Push(a); \
return 1; \
}
TABLE_TO_ARRAY_FUNC(table_keys, key)
TABLE_TO_ARRAY_FUNC(table_values, val)
const SQRegFunction SQSharedState::_table_default_delegate_funcz[]={
{_SC("len"),default_delegate_len,1, _SC("t")},
@ -512,6 +536,8 @@ const SQRegFunction SQSharedState::_table_default_delegate_funcz[]={
{_SC("setdelegate"),table_setdelegate,2, _SC(".t|o")},
{_SC("getdelegate"),table_getdelegate,1, _SC(".")},
{_SC("filter"),table_filter,2, _SC("tc")},
{_SC("keys"),table_keys,1, _SC("t") },
{_SC("values"),table_values,1, _SC("t") },
{NULL,(SQFUNCTION)0,0,NULL}
};
@ -596,16 +622,36 @@ static SQInteger array_resize(HSQUIRRELVM v)
static SQInteger __map_array(SQArray *dest,SQArray *src,HSQUIRRELVM v) {
SQObjectPtr temp;
SQInteger size = src->Size();
SQObject &closure = stack_get(v, 2);
v->Push(closure);
SQInteger nArgs=0;
if(sq_type(closure) == OT_CLOSURE) {
nArgs = _closure(closure)->_function->_nparameters;
}
else if (sq_type(closure) == OT_NATIVECLOSURE) {
SQInteger nParamsCheck = _nativeclosure(closure)->_nparamscheck;
if (nParamsCheck > 0)
nArgs = nParamsCheck;
else // push all params when there is no check or only minimal count set
nArgs = 4;
}
for(SQInteger n = 0; n < size; n++) {
src->Get(n,temp);
v->Push(src);
v->Push(temp);
if(SQ_FAILED(sq_call(v,2,SQTrue,SQFalse))) {
if (nArgs >= 3)
v->Push(SQObjectPtr(n));
if (nArgs >= 4)
v->Push(src);
if(SQ_FAILED(sq_call(v,nArgs,SQTrue,SQFalse))) {
return SQ_ERROR;
}
dest->Set(n,v->GetUp(-1));
v->Pop();
}
v->Pop();
return 0;
}
@ -634,14 +680,21 @@ static SQInteger array_reduce(HSQUIRRELVM v)
SQObject &o = stack_get(v,1);
SQArray *a = _array(o);
SQInteger size = a->Size();
if(size == 0) {
return 0;
}
SQObjectPtr res;
SQInteger iterStart;
if (sq_gettop(v)>2) {
res = stack_get(v,3);
iterStart = 0;
} else if (size==0) {
return 0;
} else {
a->Get(0,res);
if(size > 1) {
iterStart = 1;
}
if (size > iterStart) {
SQObjectPtr other;
for(SQInteger n = 1; n < size; n++) {
v->Push(stack_get(v,2));
for (SQInteger n = iterStart; n < size; n++) {
a->Get(n,other);
v->Push(o);
v->Push(res);
@ -652,6 +705,7 @@ static SQInteger array_reduce(HSQUIRRELVM v)
res = v->GetUp(-1);
v->Pop();
}
v->Pop();
}
v->Push(res);
return 1;
@ -837,7 +891,7 @@ const SQRegFunction SQSharedState::_array_default_delegate_funcz[]={
{_SC("clear"),obj_clear,1, _SC(".")},
{_SC("map"),array_map,2, _SC("ac")},
{_SC("apply"),array_apply,2, _SC("ac")},
{_SC("reduce"),array_reduce,2, _SC("ac")},
{_SC("reduce"),array_reduce,-2, _SC("ac.")},
{_SC("filter"),array_filter,2, _SC("ac")},
{_SC("find"),array_find,2, _SC("a.")},
{NULL,(SQFUNCTION)0,0,NULL}

View File

@ -940,6 +940,30 @@ public:
SQInteger stackbase = _fs->PopTarget();
SQInteger closure = _fs->PopTarget();
_fs->AddInstruction(_OP_CALL, _fs->PushTarget(), closure, stackbase, nargs);
if (_token == '{')
{
SQInteger retval = _fs->TopTarget();
SQInteger nkeys = 0;
Lex();
while (_token != '}') {
switch (_token) {
case _SC('['):
Lex(); CommaExpr(); Expect(_SC(']'));
Expect(_SC('=')); Expression();
break;
default:
_fs->AddInstruction(_OP_LOAD, _fs->PushTarget(), _fs->GetConstant(Expect(TK_IDENTIFIER)));
Expect(_SC('=')); Expression();
break;
}
if (_token == ',') Lex();
nkeys++;
SQInteger val = _fs->PopTarget();
SQInteger key = _fs->PopTarget();
_fs->AddInstruction(_OP_SET, 0xFF, retval, key, val);
}
Lex();
}
}
void ParseTableOrClass(SQInteger separator,SQInteger terminator)
{

View File

@ -641,9 +641,15 @@ bool SQVM::CLASS_OP(SQObjectPtr &target,SQInteger baseclass,SQInteger attributes
bool SQVM::IsEqual(const SQObjectPtr &o1,const SQObjectPtr &o2,bool &res)
{
if(sq_type(o1) == sq_type(o2)) {
SQObjectType t1 = sq_type(o1), t2 = sq_type(o2);
if(t1 == t2) {
if (t1 == OT_FLOAT) {
res = (_float(o1) == _float(o2));
}
else {
res = (_rawval(o1) == _rawval(o2));
}
}
else {
if(sq_isnumeric(o1) && sq_isnumeric(o2)) {
res = (tofloat(o1) == tofloat(o2));
@ -1000,6 +1006,7 @@ exception_restore:
case _OP_YIELD:{
if(ci->_generator) {
if(sarg1 != MAX_FUNC_STACKSIZE) temp_reg = STK(arg1);
if (_openouters) CloseOuters(&_stack._vals[_stackbase]);
_GUARD(ci->_generator->Yield(this,arg2));
traps -= ci->_etraps;
if(sarg1 != MAX_FUNC_STACKSIZE) _Swap(STK(arg1),temp_reg);//STK(arg1) = temp_reg;

View File

@ -59,7 +59,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#endif // SQMOD_PLUGIN_API
#include "sqrat/sqratTable.h"

View File

@ -31,7 +31,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#endif // SQMOD_PLUGIN_API
#include <string.h>

View File

@ -31,7 +31,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#endif // SQMOD_PLUGIN_API
#include <string.h>

View File

@ -31,11 +31,10 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#endif // SQMOD_PLUGIN_API
#include <typeinfo>
#include <squirrel.h>
#include "sqratObject.h"
#include "sqratClassType.h"
@ -718,7 +717,6 @@ protected:
// constructor binding
Class& BindConstructor(SQFUNCTION constructor, SQInteger nParams, const SQChar *name = 0) {
SQFUNCTION overload = &OverloadConstructionForwarder;
// Decide whether to bind to a class or the root table
bool alternative_global = false;
if (name == 0)
@ -734,12 +732,12 @@ protected:
// The containing environment is the root table??
else sq_pushroottable(vm);
// Bind overload handler
// Bind overload handler name
sq_pushstring(vm, name, -1);
// function name is passed as a free variable
//sq_pushstring(vm, name, -1);
sq_push(vm, -1); // <- Let's cheat(?) by pushing the same object
sq_newclosure(vm, overload, 1);
sq_newclosure(vm, &OverloadConstructionForwarder, 1);
// Set the closure name (for debug purposes)
sq_setnativeclosurename(vm, -1, name);
// Include it into the object

View File

@ -31,7 +31,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#endif // SQMOD_PLUGIN_API
#include <typeinfo>

View File

@ -31,7 +31,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#endif // SQMOD_PLUGIN_API
#include <string.h>

View File

@ -32,7 +32,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#endif // SQMOD_PLUGIN_API
#include "sqratObject.h"

View File

@ -32,7 +32,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#endif // SQMOD_PLUGIN_API
#include "sqratTypes.h"

View File

@ -32,7 +32,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#endif // SQMOD_PLUGIN_API
#include "sqratTypes.h"

View File

@ -31,7 +31,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#endif // SQMOD_PLUGIN_API
#include <string.h>

View File

@ -31,7 +31,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#endif // SQMOD_PLUGIN_API
#include "sqratTypes.h"

View File

@ -31,7 +31,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#include <sqstdio.h>
#endif // SQMOD_PLUGIN_API

View File

@ -31,7 +31,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#endif // SQMOD_PLUGIN_API
#include <string.h>

View File

@ -34,7 +34,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#endif // SQMOD_PLUGIN_API
#include <string>

View File

@ -31,7 +31,7 @@
#ifdef SQMOD_PLUGIN_API
#include <SqAPI.h>
#else
#include <squirrel.h>
#include <squirrelex.h>
#include <sqstdstring.h>
#endif // SQMOD_PLUGIN_API

View File

@ -9,6 +9,8 @@ extern "C" {
SQUIRREL_API void sqstd_seterrorhandlers(HSQUIRRELVM v);
SQUIRREL_API void sqstd_printcallstack(HSQUIRRELVM v);
SQUIRREL_API SQRESULT sqstd_throwerrorf(HSQUIRRELVM v,const SQChar *err,...);
#ifdef __cplusplus
} /*extern "C"*/
#endif

View File

@ -24,6 +24,8 @@ SQUIRREL_API SQBool sqstd_rex_getsubexp(SQRex* exp, SQInteger n, SQRexMatch *sub
SQUIRREL_API SQRESULT sqstd_format(HSQUIRRELVM v,SQInteger nformatstringidx,SQInteger *outlen,SQChar **output);
SQUIRREL_API void sqstd_pushstringf(HSQUIRRELVM v,const SQChar *s,...);
SQUIRREL_API SQRESULT sqstd_register_stringlib(HSQUIRRELVM v);
#ifdef __cplusplus

View File

@ -281,7 +281,6 @@ SQUIRREL_API SQRESULT sq_getfunctioninfo(HSQUIRRELVM v,SQInteger level,SQFunctio
SQUIRREL_API SQRESULT sq_getclosureinfo(HSQUIRRELVM v,SQInteger idx,SQInteger *nparams,SQInteger *nfreevars);
SQUIRREL_API SQRESULT sq_getclosurename(HSQUIRRELVM v,SQInteger idx);
SQUIRREL_API SQRESULT sq_setnativeclosurename(HSQUIRRELVM v,SQInteger idx,const SQChar *name);
SQUIRREL_API SQRESULT sq_getnativeclosurepointer(HSQUIRRELVM v,SQInteger idx,SQFUNCTION *f);
SQUIRREL_API SQRESULT sq_setinstanceup(HSQUIRRELVM v, SQInteger idx, SQUserPointer p);
SQUIRREL_API SQRESULT sq_getinstanceup(HSQUIRRELVM v, SQInteger idx, SQUserPointer *p,SQUserPointer typetag);
SQUIRREL_API SQRESULT sq_setclassudsize(HSQUIRRELVM v, SQInteger idx, SQInteger udsize);

25
include/squirrelex.h Normal file
View File

@ -0,0 +1,25 @@
/*
see copyright notice in squirrel.h
*/
#ifndef _SQUIRRELEX_H_
#define _SQUIRRELEX_H_
#include <squirrel.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef SQMOD_PLUGIN_API
SQUIRREL_API SQRESULT sq_throwerrorf(HSQUIRRELVM v,const SQChar *err,...);
SQUIRREL_API void sq_pushstringf(HSQUIRRELVM v,const SQChar *s,...);
SQUIRREL_API SQRESULT sq_getnativeclosurepointer(HSQUIRRELVM v,SQInteger idx,SQFUNCTION *f);
#endif // SQMOD_PLUGIN_API
#ifdef __cplusplus
} /*extern "C"*/
#endif
#endif /*_SQUIRRELEX_H_*/

View File

@ -2,7 +2,7 @@
#define _REGISTER_HPP_
// ------------------------------------------------------------------------------------------------
#include <squirrel.h>
#include <squirrelex.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {