Eiyeron Fulmincendii пре 9 година
родитељ
комит
03ae83c541

+ 15 - 0
doc/source/reference/api/object_creation_and_handling.rst

@@ -243,6 +243,21 @@ gets a pointer to the string at the idx position in the stack.
 
 
 
+.. _sq_getstringandsize:
+
+.. c:function:: SQRESULT sq_getstringandsize(HSQUIRRELVM v, SQInteger idx, const SQChar ** c, SQInteger* size)
+
+    :param HSQUIRRELVM v: the target VM
+    :param SQInteger idx: an index in the stack
+    :param const SQChar ** c: a pointer to the pointer that will point to the string
+    :param SQInteger * size: a pointer to a SQInteger which will receive the size of the string
+    :returns: a SQRESULT
+
+gets a pointer to the string at the idx position in the stack; additionally retrieves its size.
+
+
+
+
 .. _sq_getthread:
 
 .. c:function:: SQRESULT sq_getthread(HSQUIRRELVM v, SQInteger idx, HSQUIRRELVM* v)

+ 7 - 7
doc/source/reference/api/object_manipulation.rst

@@ -135,7 +135,7 @@ Clones the table, array or class instance at the position idx, clones it and pus
     :returns: a SQRESULT
     :remarks: invoke the _newslot metamethod in the table delegate. it only works on tables. [this function is deperecated since version 2.0.5 use sq_newslot() instead]
 
-pops a key and a value from the stack and performs a set operation on the table or class that is at position idx in the stack, if the slot does not exits it will be created.
+pops a key and a value from the stack and performs a set operation on the table or class that is at position idx in the stack; if the slot does not exist, it will be created.
 
 
 
@@ -151,7 +151,7 @@ pops a key and a value from the stack and performs a set operation on the table
     :returns: a SQRESULT
     :remarks: invoke the _delslot metamethod in the table delegate. it only works on tables.
 
-pops a key from the stack and delete the slot indexed by it from the table at position idx in the stack, if the slot does not exits nothing happens.
+pops a key from the stack and delete the slot indexed by it from the table at position idx in the stack; if the slot does not exist, nothing happens.
 
 
 
@@ -164,7 +164,7 @@ pops a key from the stack and delete the slot indexed by it from the table at po
     :param HSQUIRRELVM v: the target VM
     :param SQInteger idx: index of the target object in the stack
     :returns: a SQRESULT
-    :remarks: this call will invokes the delegation system like a normal dereference it only works on tables, arrays and userdata. If the function fails, nothing will be pushed in the stack.
+    :remarks: this call will invokes the delegation system like a normal dereference it only works on tables, arrays and userdata; if the function fails, nothing will be pushed in the stack.
 
 pops a key from the stack and performs a get operation on the object at the position idx in the stack; and pushes the result in the stack.
 
@@ -283,7 +283,7 @@ Determines if an object is an instance of a certain class. Expects an instance a
     :returns: a SQRESULT
     :remarks: Invokes the _newmember metamethod in the class. it only works on classes.
 
-pops a key, a value and an object (which will be set as attribute of the member) from the stack and performs a new slot operation on the class that is at position idx in the stack; if the slot does not exits it will be created.
+pops a key, a value and an object (which will be set as attribute of the member) from the stack and performs a new slot operation on the class that is at position idx in the stack; if the slot does not exist, it will be created.
 
 
 
@@ -299,7 +299,7 @@ pops a key, a value and an object (which will be set as attribute of the member)
     :returns: a SQRESULT
     :remarks: Invokes the _newslot metamethod in the table delegate. it only works on tables and classes.
 
-pops a key and a value from the stack and performs a set operation on the table or class that is at position idx in the stack, if the slot does not exits it will be created.
+pops a key and a value from the stack and performs a set operation on the table or class that is at position idx in the stack, if the slot does not exist it will be created.
 
 
 
@@ -328,7 +328,7 @@ Pushes in the stack the next key and value of an array, table, or class slot. To
     :param SQBool pushval: if this param is true the function will push the value of the deleted slot.
     :returns: a SQRESULT
 
-Deletes a slot from a table without employing the _delslot metamethod. Pops a key from the stack and delete the slot indexed by it from the table at position idx in the stack, if the slot does not exits nothing happens.
+Deletes a slot from a table without employing the _delslot metamethod. Pops a key from the stack and delete the slot indexed by it from the table at position idx in the stack; if the slot does not exist nothing happens.
 
 
 
@@ -359,7 +359,7 @@ pops a key from the stack and performs a get operation on the object at position
     :returns: a SQRESULT
     :remarks: it only works on classes.
 
-pops a key, a value and an object(that will be set as attribute of the member) from the stack and performs a new slot operation on the class that is at position idx in the stack, if the slot does not exits it will be created.
+pops a key, a value and an object(that will be set as attribute of the member) from the stack and performs a new slot operation on the class that is at position idx in the stack; if the slot does not exist it will be created.
 
 
 

+ 1 - 0
doc/source/reference/embedding/the_stack.rst

@@ -92,6 +92,7 @@ the result can be one of the following values: ::
 The following functions convert a squirrel value in the stack to a C value::
 
     SQRESULT sq_getstring(HSQUIRRELVM v,SQInteger idx,const SQChar **c);
+    SQRESULT sq_getstringandsize(HSQUIRRELVM v,SQInteger idx,const SQChar **c,SQInteger size);
     SQRESULT sq_getinteger(HSQUIRRELVM v,SQInteger idx,SQInteger *i);
     SQRESULT sq_getfloat(HSQUIRRELVM v,SQInteger idx,SQFloat *f);
     SQRESULT sq_getuserpointer(HSQUIRRELVM v,SQInteger idx,SQUserPointer *p);

+ 2 - 2
doc/source/reference/language/builtin_functions.rst

@@ -52,7 +52,7 @@ returns the const table of the VM.
 
 .. js:function:: setconsttable(table)
 
-sets the const table of the VM. And returns the previous const table.
+sets the const table of the VM; returns the previous const table.
 
 .. js:function:: assert(exp)
 
@@ -60,7 +60,7 @@ throws an exception if exp is null
 
 .. js:function:: print(x)
 
-prints x in the standard output
+prints x to the standard output
 
 .. js:function:: error(x)
 

+ 15 - 15
doc/source/reference/language/metamethods.rst

@@ -63,7 +63,7 @@ _set
     _set(idx,val)
 
 invoked when the index idx is not present in the object or in its delegate chain.
-``_set`` must 'throw null' to notify that a key wasn't found but the there were not runtime errors(clean failure).
+``_set`` must 'throw null' to notify that a key wasn't found but the there were not runtime errors (clean failure).
 This allows the program to differentiate between a runtime error and a 'index not found'.
 
 ^^^^^
@@ -75,7 +75,7 @@ _get
     _get(idx)
 
 invoked when the index idx is not present in the object or in its delegate chain.
-_get must 'throw null' to notify that a key wasn't found but the there were not runtime errors(clean failure).
+_get must 'throw null' to notify that a key wasn't found but the there were not runtime errors (clean failure).
 This allows the program to differentiate between a runtime error and a 'index not found'.
 
 ^^^^^^^^^
@@ -165,16 +165,16 @@ _unm
 the unary minus operator
 
 ^^^^^^^^^^^^^^^^^^^^^^^^
-_tyoeof
+_typeof
 ^^^^^^^^^^^^^^^^^^^^^^^^
 
 ::
 
-    _tyoeof()
+    _typeof()
 
-invoked by the typeof operator on tables, userdata and class instances
+invoked by the typeof operator on tables, userdata, and class instances.
 
-returns the type of ``this`` as string
+Returns the type of ``this`` as string
 
 ^^^^^^^^^^^^^^^^^^^^^^^^
 _cmp
@@ -228,7 +228,7 @@ _nexti
 
 invoked when a userdata or class instance is iterated by a foreach loop
 
-if previdx==null it means that it is the first iteration.
+If previdx==null it means that it is the first iteration.
 The function has to return the index of the 'next' value.
 
 ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -239,10 +239,10 @@ _tostring
 
     _tostring(previdx)
 
-invoked when during string conacatenation or when the ``print`` function prints a table, instance, or userdata.
-The method is also invoked by the sq_tostring() api
+invoked when during string concatenation or when the ``print`` function prints a table, instance, or userdata.
+The method is also invoked by the sq_tostring() API
 
-must return a string representation of the object.
+Must return a string representation of the object.
 
 ^^^^^^^^^^^^^^^^^^^^^^^^
 _inherited
@@ -252,10 +252,10 @@ _inherited
 
     _inherited(attributes)
 
-invoked when a class object inherits from the class implementing ``_inherited``
-the ``this`` contains the new class.
+invoked when a class object inherits from the class implementing ``_inherited``.
+The ``this`` contains the new class.
 
-return value is ignored.
+Return value is ignored.
 
 ^^^^^^^^^^^^^^^^^^^^^^^^
 _newmember
@@ -265,6 +265,6 @@ _newmember
 
     _newmember(index,value,attributes,isstatic)
 
-invoked for each member declared in a class body(at declaration time).
+invoked for each member declared in a class body (at declaration time).
 
-if the function is implemented, members will not be added to the class.
+If the function is implemented, members will not be added to the class.

+ 10 - 2
doc/source/stdlib/stdstringlib.rst

@@ -18,7 +18,7 @@ Global Symbols
 
     returns `true` if the end of the string `str`  matches a the string `cmp` otherwise returns `false`
 	
-.. js:function:: ecape(str)
+.. js:function:: escape(str)
 
     Returns a string with backslashes before characters that need to be escaped(`\",\a,\b,\t,\n,\v,\f,\r,\\,\",\',\0,\xnn`).
 
@@ -28,10 +28,18 @@ Global Symbols
     The format string follows the same rules as the `printf` family of
     standard C functions( the "*" is not supported). ::
 
-        eg.
+        e.g.
         sq> print(format("%s %d 0x%02X\n","this is a test :",123,10));
         this is a test : 123 0x0A
 
+.. js:function:: printf(formatstr, ...)
+
+    Just like calling `print(format(formatstr` as in the example above, but is more convenient AND more efficient. ::
+
+        e.g.
+        sq> printf("%s %d 0x%02X\n","this is a test :",123,10);
+        this is a test : 123 0x0A
+
 .. js:function:: lstrip(str)
 
     Strips white-space-only characters that might appear at the beginning of the given string

+ 1 - 0
include/squirrel.h

@@ -258,6 +258,7 @@ SQUIRREL_API SQRESULT sq_getbase(HSQUIRRELVM v,SQInteger idx);
 SQUIRREL_API SQBool sq_instanceof(HSQUIRRELVM v);
 SQUIRREL_API SQRESULT sq_tostring(HSQUIRRELVM v,SQInteger idx);
 SQUIRREL_API void sq_tobool(HSQUIRRELVM v, SQInteger idx, SQBool *b);
+SQUIRREL_API SQRESULT sq_getstringandsize(HSQUIRRELVM v,SQInteger idx,const SQChar **c,SQInteger *size);
 SQUIRREL_API SQRESULT sq_getstring(HSQUIRRELVM v,SQInteger idx,const SQChar **c);
 SQUIRREL_API SQRESULT sq_getinteger(HSQUIRRELVM v,SQInteger idx,SQInteger *i);
 SQUIRREL_API SQRESULT sq_getfloat(HSQUIRRELVM v,SQInteger idx,SQFloat *f);

+ 14 - 0
sqstdlib/sqstdstring.cpp

@@ -150,6 +150,19 @@ SQRESULT sqstd_format(HSQUIRRELVM v,SQInteger nformatstringidx,SQInteger *outlen
     return SQ_OK;
 }
 
+static SQInteger _string_printf(HSQUIRRELVM v)
+{
+    SQChar *dest = NULL;
+    SQInteger length = 0;
+    if(SQ_FAILED(sqstd_format(v,2,&length,&dest)))
+        return -1;
+    
+    SQPRINTFUNCTION printfunc = sq_getprintfunc(v);
+    if(printfunc) printfunc(v,dest);
+
+    return 0;
+}
+
 static SQInteger _string_format(HSQUIRRELVM v)
 {
     SQChar *dest = NULL;
@@ -459,6 +472,7 @@ static const SQRegFunction rexobj_funcs[]={
 #define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_string_##name,nparams,pmask}
 static const SQRegFunction stringlib_funcs[]={
     _DECL_FUNC(format,-2,_SC(".s")),
+    _DECL_FUNC(printf,-2,_SC(".s")),
     _DECL_FUNC(strip,2,_SC(".s")),
     _DECL_FUNC(lstrip,2,_SC(".s")),
     _DECL_FUNC(rstrip,2,_SC(".s")),

+ 9 - 0
squirrel/sqapi.cpp

@@ -680,6 +680,15 @@ SQRESULT sq_getbool(HSQUIRRELVM v,SQInteger idx,SQBool *b)
     return SQ_ERROR;
 }
 
+SQRESULT sq_getstringandsize(HSQUIRRELVM v,SQInteger idx,const SQChar **c,SQInteger *size)
+{
+    SQObjectPtr *o = NULL;
+    _GETSAFE_OBJ(v, idx, OT_STRING,o);
+    *c = _stringval(*o);
+    *size = _string(*o)->_len;
+    return SQ_OK;
+}
+
 SQRESULT sq_getstring(HSQUIRRELVM v,SQInteger idx,const SQChar **c)
 {
     SQObjectPtr *o = NULL;

+ 21 - 31
squirrel/sqstate.cpp

@@ -12,12 +12,6 @@
 #include "squserdata.h"
 #include "sqclass.h"
 
-//SQObjectPtr _null_;
-//SQObjectPtr _true_(true);
-//SQObjectPtr _false_(false);
-//SQObjectPtr _one_((SQInteger)1);
-//SQObjectPtr _minusone_((SQInteger)-1);
-
 SQSharedState::SQSharedState()
 {
     _compilererrorhandler = NULL;
@@ -41,31 +35,29 @@ SQSharedState::SQSharedState()
 bool CompileTypemask(SQIntVec &res,const SQChar *typemask)
 {
     SQInteger i = 0;
-
     SQInteger mask = 0;
     while(typemask[i] != 0) {
-
-        switch(typemask[i]){
-                case 'o': mask |= _RT_NULL; break;
-                case 'i': mask |= _RT_INTEGER; break;
-                case 'f': mask |= _RT_FLOAT; break;
-                case 'n': mask |= (_RT_FLOAT | _RT_INTEGER); break;
-                case 's': mask |= _RT_STRING; break;
-                case 't': mask |= _RT_TABLE; break;
-                case 'a': mask |= _RT_ARRAY; break;
-                case 'u': mask |= _RT_USERDATA; break;
-                case 'c': mask |= (_RT_CLOSURE | _RT_NATIVECLOSURE); break;
-                case 'b': mask |= _RT_BOOL; break;
-                case 'g': mask |= _RT_GENERATOR; break;
-                case 'p': mask |= _RT_USERPOINTER; break;
-                case 'v': mask |= _RT_THREAD; break;
-                case 'x': mask |= _RT_INSTANCE; break;
-                case 'y': mask |= _RT_CLASS; break;
-                case 'r': mask |= _RT_WEAKREF; break;
-                case '.': mask = -1; res.push_back(mask); i++; mask = 0; continue;
-                case ' ': i++; continue; //ignores spaces
-                default:
-                    return false;
+        switch(typemask[i]) {
+            case 'o': mask |= _RT_NULL; break;
+            case 'i': mask |= _RT_INTEGER; break;
+            case 'f': mask |= _RT_FLOAT; break;
+            case 'n': mask |= (_RT_FLOAT | _RT_INTEGER); break;
+            case 's': mask |= _RT_STRING; break;
+            case 't': mask |= _RT_TABLE; break;
+            case 'a': mask |= _RT_ARRAY; break;
+            case 'u': mask |= _RT_USERDATA; break;
+            case 'c': mask |= (_RT_CLOSURE | _RT_NATIVECLOSURE); break;
+            case 'b': mask |= _RT_BOOL; break;
+            case 'g': mask |= _RT_GENERATOR; break;
+            case 'p': mask |= _RT_USERPOINTER; break;
+            case 'v': mask |= _RT_THREAD; break;
+            case 'x': mask |= _RT_INSTANCE; break;
+            case 'y': mask |= _RT_CLASS; break;
+            case 'r': mask |= _RT_WEAKREF; break;
+            case '.': mask = -1; res.push_back(mask); i++; mask = 0; continue;
+            case ' ': i++; continue; //ignores spaces
+            default:
+                return false;
         }
         i++;
         if(typemask[i] == '|') {
@@ -160,7 +152,6 @@ void SQSharedState::Init()
     _class_default_delegate = CreateDefaultDelegate(this,_class_default_delegate_funcz);
     _instance_default_delegate = CreateDefaultDelegate(this,_instance_default_delegate_funcz);
     _weakref_default_delegate = CreateDefaultDelegate(this,_weakref_default_delegate_funcz);
-
 }
 
 SQSharedState::~SQSharedState()
@@ -282,7 +273,6 @@ SQInteger SQSharedState::ResurrectUnreachable(SQVM *vm)
 
     SQCollectable *resurrected = _gc_chain;
     SQCollectable *t = resurrected;
-    //SQCollectable *nx = NULL;
 
     _gc_chain = tchain;
 

+ 3 - 2
squirrel/sqvm.cpp

@@ -15,6 +15,8 @@
 #include "sqclass.h"
 
 #define TOP() (_stack._vals[_top-1])
+#define TARGET _stack._vals[_stackbase+arg0]
+#define STK(a) _stack._vals[_stackbase+(a)]
 
 bool SQVM::BW_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2)
 {
@@ -1367,9 +1369,8 @@ SQInteger SQVM::FallBackSet(const SQObjectPtr &self,const SQObjectPtr &key,const
                 return FALLBACK_OK;
             }
             else {
+                Pop(3);
                 if(type(_lasterror) != OT_NULL) { //NULL means "clean failure" (not found)
-                    //error
-                    Pop(3);
                     return FALLBACK_ERROR;
                 }
             }

+ 0 - 3
squirrel/sqvm.h

@@ -28,9 +28,6 @@ struct SQExceptionTrap{
 
 #define _INLINE
 
-#define STK(a) _stack._vals[_stackbase+(a)]
-#define TARGET _stack._vals[_stackbase+arg0]
-
 typedef sqvector<SQExceptionTrap> ExceptionsTraps;
 
 struct SQVM : public CHAINABLE_OBJ