Explorar o código

Add a global function "table_toarray"

mingodad %!s(int64=6) %!d(string=hai) anos
pai
achega
c9c778f18f
Modificáronse 2 ficheiros con 33 adicións e 1 borrados
  1. 23 1
      SquiLu/squirrel/sqbaselib.cpp
  2. 10 0
      SquiLu/tests/squilu-test.nut

+ 23 - 1
SquiLu/squirrel/sqbaselib.cpp

@@ -528,6 +528,27 @@ static SQRESULT bf_table_incnum(HSQUIRRELVM v)
 	return 1;
 }
 
+static SQRESULT bf_table_toarray(HSQUIRRELVM v)
+{
+	SQObjectPtr &self = stack_get(v, 2);
+	SQInteger tsz = sq_getsize(v, 2);
+	sq_newarray(v, tsz);
+	SQInteger idx = 0;
+    sq_pushnull(v);
+    while(sq_next(v, 2) == SQ_OK)
+    {
+        sq_newarray(v, 2);
+        sq_push(v, -3);
+        sq_arrayset(v, -2, 0); //table key
+        sq_push(v, -2);
+        sq_arrayset(v, -2, 1); //table value
+        sq_arrayset(v, 3, idx++); //set the new array into ary
+        sq_pop(v, 2);
+    }
+    sq_pop(v, 1); //the null before while
+	return 1;
+}
+
 static SQRESULT bf_obj_clone(HSQUIRRELVM v)
 {
     SQRESULT rc = sq_clone(v,-1);
@@ -664,6 +685,7 @@ static SQRegFunction base_funcs[]={
 	{_SC("table_getdelegate"),bf_table_getdelegate,2, _SC(".t"), false},
 	{_SC("table_getdelegate_squirrel"),bf_table_getdelegate_squirrel,1, _SC("."), false},
 	{_SC("table_incnum"),bf_table_incnum,-4, _SC(".tsnb"), false},
+	{_SC("table_toarray"),bf_table_toarray,2, _SC(".t"), false},
 	{_SC("obj_clone"),bf_obj_clone,2, _SC(". t|a|x|i|f|s"), false},
 	{NULL,(SQFUNCTION)0,0,NULL, false}
 };
@@ -2626,7 +2648,7 @@ SQRegFunction SQSharedState::_string_default_delegate_funcz[]={
 	{_SC("iso88959_to_utf8"),string_iso88959_to_utf8,1, _SC("s"), false},
 	{_SC("isvalidutf8"),string_isvalidutf8,1, _SC("s"), false},
 	{_SC("longestcommonsubstr"),string_longestcommonsubstr,2, _SC("ss"), false},
-	
+
 #ifdef SQ_SUBLATIN
 	{_SC("sl_len"),string_sl_len,1, _SC("s"), false},
 	{_SC("sl_lower"),string_sl_lower,1, _SC("s"), false},

+ 10 - 0
SquiLu/tests/squilu-test.nut

@@ -968,9 +968,19 @@ sqt.run("table", function(){
 	sqt.ok((table_get(t, "one") == 10));
 	table_rawset(t, "two", 2);
 	sqt.ok(table_len(t) == 2 && (table_get(t, "two") == 2));
+
+	local ary = table_toarray(t);
+	sqt.ok(type(ary) == "array");
+	sqt.ok(ary.len() == table_len(t));
+	sqt.ok(type(ary[0]) == "array");
+	sqt.ok(type(ary[1]) == "array");
+	sqt.ok(ary[0][1] == table_rawget(t, ary[0][0]));
+	sqt.ok(ary[1][1] == table_rawget(t, ary[1][0]));
+	
 	table_rawdelete(t, "one");
 	sqt.ok(table_len(t) == 1 && (table_get(t, "two") == 2));
 	sqt.ok(table_rawin(t, "two"));
+	
 	table_clear(t);
 	sqt.ok(table_len(t) == 0);