Browse Source

added table.keys() and table.values()

albertodemichelis 6 years ago
parent
commit
6bf9ab5162
3 changed files with 40 additions and 1 deletions
  1. 6 1
      HISTORY
  2. 8 0
      doc/source/reference/language/builtin_functions.rst
  3. 26 0
      squirrel/sqbaselib.cpp

+ 6 - 1
HISTORY

@@ -1,6 +1,11 @@
 ***version 3.2 stable***
 ***version 3.2 stable***
 -added sq_tailcall
 -added sq_tailcall
 -added rawcall keyword
 -added rawcall keyword
+-added post call initializer syntax
+-added table.keys() and table.values()
+-added table.filter()
+-additional parameters in array.map() and array.apply()
+-additional optional initializer in array.reduce()
 -closure.call() is now a "native tailcall" and the invoked function can now be suspended
 -closure.call() is now a "native tailcall" and the invoked function can now be suspended
 -fixed sq_newmember and sq_rawnewmember properly pop parameters
 -fixed sq_newmember and sq_rawnewmember properly pop parameters
 -fixed capturing free variable on for loop counter before a break statement
 -fixed capturing free variable on for loop counter before a break statement
@@ -9,7 +14,7 @@
 
 
 ***version 3.1.1 stable***
 ***version 3.1.1 stable***
 -sq_gettypetag doesn't set last error(it's treated as SQBool function but keeps a SQRESULT for backward compatibility)
 -sq_gettypetag doesn't set last error(it's treated as SQBool function but keeps a SQRESULT for backward compatibility)
--fixed _set method in userdata deelegates
+-fixed _set method in userdata delegates
 -fixed some warnings
 -fixed some warnings
 
 
 ***version 3.1 stable***
 ***version 3.1 stable***

+ 8 - 0
doc/source/reference/language/builtin_functions.rst

@@ -321,6 +321,14 @@ returns the table's delegate or null if no delegate was set.
 
 
 Creates a new table with all values that pass the test implemented by the provided function. In detail, it creates a new table, invokes the specified function for each key-value pair in the original table; if the function returns 'true', then the value is added to the newly created table at the same key.
 Creates a new table with all values that pass the test implemented by the provided function. In detail, it creates a new table, invokes the specified function for each key-value pair in the original table; if the function returns 'true', then the value is added to the newly created table at the same key.
 
 
+.. js:function:: table.keys()
+
+returns an array containing all the keys of the table slots.
+
+.. js:function:: table.values()
+
+returns an array containing all the values of the table slots.
+
 ^^^^^^
 ^^^^^^
 Array
 Array
 ^^^^^^
 ^^^^^^

+ 26 - 0
squirrel/sqbaselib.cpp

@@ -499,6 +499,30 @@ static SQInteger table_filter(HSQUIRRELVM v)
     return 1;
     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[]={
 const SQRegFunction SQSharedState::_table_default_delegate_funcz[]={
     {_SC("len"),default_delegate_len,1, _SC("t")},
     {_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("setdelegate"),table_setdelegate,2, _SC(".t|o")},
     {_SC("getdelegate"),table_getdelegate,1, _SC(".")},
     {_SC("getdelegate"),table_getdelegate,1, _SC(".")},
     {_SC("filter"),table_filter,2, _SC("tc")},
     {_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}
     {NULL,(SQFUNCTION)0,0,NULL}
 };
 };