YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Setting all of null structure members
Alikberov
#1 Posted : Wednesday, March 1, 2017 8:47:43 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/1/2017(UTC)
Posts: 5
Location: Asia/Uzbekistan/Tashkent

Thanks: 1 times
Was thanked: 0 time(s) in 0 post(s)
Hi!
How can I set the some member of structure? Any of my ways still not working.
Common plan is included a special function for synchronize data pattern.

As example of my difficults, Please looking for this short working example:
Code:
#include "stdafx.h"

#include&#160;<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdarg.h>

#include&#160;"../../SQUIRREL3/include/squirrel.h"
#include&#160;"../../SQUIRREL3/include/sqstdio.h"
#include&#160;"../../SQUIRREL3/include/sqstdaux.h"
#ifdef&#160;_MSC_VER
#pragma&#160;comment&#160;(lib&#160;,"../../SQUIRREL3/lib/squirrel.lib")
#pragma&#160;comment&#160;(lib&#160;,"../../SQUIRREL3/lib/sqstdlib.lib")
#endif

HSQUIRRELVM&#160;svm;

SQInteger&#160;&#160;&#160;SQF_Test(HSQUIRRELVM&#160;v)&#160;{
&#160;&#160;&#160;&#160;SQInteger&#160;&#160;&#160;&#160;&#160;&#160;&#160;iArg&#160;=&#160;sq_gettop(v);&#160;//number&#160;of&#160;arguments
&#160;&#160;&#160;&#160;SQInteger&#160;&#160;&#160;&#160;&#160;&#160;&#160;iValue,&#160;jValue,&#160;&#160;&#160;&#160;&#160;i,&#160;j;
&#160;&#160;&#160;&#160;SQChar&#160;const&#160;&#160;&#160;&#160;*pszValue;
&#160;&#160;&#160;&#160;char&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sz[64]&#160;=&#160;{0};
&#160;&#160;&#160;&#160;wchar_t&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;wsz[64];
&#160;&#160;&#160;&#160;SQMemberHandle&#160;&#160;sqh;
&#160;&#160;&#160;&#160;union&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SQChar&#160;const&#160;&#160;&#160;&#160;*pszKey;
&#160;&#160;&#160;&#160;};
&#160;&#160;&#160;&#160;while(iArg&#160;>&#160;1)&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;switch(sq_gettype(v,&#160;iArg))&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;case&#160;OT_TABLE:
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_pushnull(v);&#160;&#160;//null&#160;iterator
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;while(SQ_SUCCEEDED(sq_next(v,&#160;-2)))&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;switch(sq_gettype(v,&#160;-2))&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;case&#160;OT_STRING:
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_getstring(v,&#160;-2,&#160;&pszKey);
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if(!strcmp(pszKey,&#160;"x"))&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;//&#160;Here&#160;are&#160;starting&#160;very&#160;problemmed&#160;code,
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;//&#160;that&#160;I'm&#160;still&#160;can't&#160;right&#160;present...
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;//&#160;Target:&#160;Set&#160;the&#160;123-number&#160;to&#160;a&#160;x-member
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_pushstring(svm,&#160;"x",&#160;-3);
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_pushinteger(svm,&#160;123);
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_rawset(svm,&#160;-5);
//&#160;Here&#160;are&#160;very&#160;dumped&#160;variations&#160;of&#160;all,
//&#160;that&#160;I'm&#160;finding&#160;with&#160;Google&#160;in&#160;different&#160;sites...
//&#160;But,&#160;any&#160;way,&#160;that&#160;-&#160;still&#160;not&#160;work!
//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_set(v,&#160;-1);
/*
HSQOBJECT&#160;hsq_obj;
//sq_getstackobj(v,&#160;-1,&#160;&retObj);
//sq_objtointeger(&retObj);
//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SQ_SUCCEEDED(sq_getmemberhandle(v,&#160;-2,&#160;&sqh));
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;//sq_pushstring(v,&#160;"x",&#160;-1),&#160;
//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_pushinteger(v,&#160;123);
//&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_setattributes(v,&#160;-1);
//sq_resetobject(&hsq_obj);&#160;//initialize&#160;the&#160;handle
sq_getstackobj(v,-1,&hsq_obj);&#160;//retrieve&#160;an&#160;object&#160;handle&#160;from&#160;the&#160;pos&#160;–2
hsq_obj._unVal.nInteger&#160;++;
sq_pushobject(v,&#160;hsq_obj);
sq_rawset(v,&#160;-1);
sq_rawset(v,&#160;-2);
sq_rawset(v,&#160;-3);
//sq_addref(v,&hsq_obj);&#160;//adds&#160;a&#160;reference&#160;to&#160;the&#160;object
//sq_pushinteger(v,&#160;123);
//sq_rawset(v,-1);
*/

&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}&#160;else
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;break;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_pop(v,2);&#160;//pops&#160;key&#160;and&#160;val&#160;before&#160;the&#160;nex&#160;iteration
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_pop(v,&#160;2);
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;break;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;//sq_pop(v,&#160;1);
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;--&#160;iArg;
&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;return&#160;1;
}

struct&#160;{
&#160;&#160;&#160;&#160;char&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;*alias;
&#160;&#160;&#160;&#160;SQInteger&#160;&#160;&#160;(*function)(HSQUIRRELVM);
}&#160;alf[]&#160;=&#160;{
&#160;&#160;&#160;&#160;"Test"&#160;&#160;&#160;&#160;&#160;&#160;,SQF_Test,
&#160;&#160;&#160;&#160;NULL,&#160;&#160;&#160;NULL
};

SQInteger&#160;SQ_register_global_func(HSQUIRRELVM&#160;v,&#160;SQFUNCTION&#160;f,&#160;char&#160;const&#160;*fname)&#160;{
&#160;&#160;&#160;&#160;sq_pushroottable(v);
&#160;&#160;&#160;&#160;sq_pushstring(v,fname,-1);
&#160;&#160;&#160;&#160;sq_newclosure(v,f,0);&#160;//create&#160;a&#160;new&#160;function
&#160;&#160;&#160;&#160;sq_newslot(v,-3,SQFalse);&#160;
&#160;&#160;&#160;&#160;sq_pop(v,1);&#160;//pops&#160;the&#160;root&#160;table&#160;&#160;&#160;&#160;
&#160;&#160;&#160;&#160;return&#160;0;
}

void&#160;printfunc(HSQUIRRELVM&#160;v,&#160;const&#160;SQChar&#160;*s,&#160;...)&#160;{
&#160;&#160;&#160;&#160;char&#160;&#160;&#160;&#160;sz[4096];
&#160;&#160;&#160;&#160;va_list&#160;vl;
&#160;&#160;&#160;&#160;va_start(vl,&#160;s);
&#160;&#160;&#160;&#160;vprintf(s,&#160;vl);
&#160;&#160;&#160;&#160;va_end(vl);
}

void&#160;errorfunc(HSQUIRRELVM&#160;v,&#160;const&#160;SQChar&#160;*s,&#160;...)&#160;{
&#160;&#160;&#160;&#160;char&#160;&#160;&#160;&#160;sz[4096];
&#160;&#160;&#160;&#160;va_list&#160;vl;
&#160;&#160;&#160;&#160;va_start(vl,&#160;s);
&#160;&#160;&#160;&#160;vprintf(s,&#160;vl);
&#160;&#160;&#160;&#160;va_end(vl);
}

SQChar&#160;&#160;szCmd[]&#160;=
"//\
\r\n&#160;&#160;&#160;&#160;function&#160;TeSt(_)&#160;{\
\r\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;_.x&#160;=&#160;456;\
\r\n&#160;&#160;&#160;&#160;}\
\r\n&#160;&#160;&#160;&#160;::print(\"BEGIN\\r\\n\");\
\r\n&#160;&#160;&#160;&#160;local&#160;&#160;&#160;q&#160;=&#160;{\
\r\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;x&#160;=&#160;null\
\r\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;};\
\r\n&#160;&#160;&#160;&#160;::print(\"Initial&#160;status:&#160;q.x&#160;=&#160;\"&#160;+&#160;q.x&#160;+&#160;\"\\r\\n\");\
\r\n//&#160;Write&#160;to&#160;console&#160;\"q&#160;=&#160;(null&#160;:&#160;0x00000000)&#160;...\"&#160;-&#160;right!\
\r\n&#160;&#160;&#160;&#160;Test(q);&#160;//&#160;Here&#160;are&#160;q.x&#160;is&#160;setted&#160;to&#160;a&#160;integer&#160;123\
\r\n&#160;&#160;&#160;&#160;::print(\">After&#160;Test(q):&#160;q.x&#160;=&#160;\"&#160;+&#160;q.x&#160;+&#160;\"\\r\\n\");\
\r\n//&#160;Write&#160;to&#160;console&#160;still&#160;\"q&#160;=&#160;(null&#160;:&#160;0x00000000)\"&#160;-&#160;wrong!!!&#160;(Where&#160;are&#160;123???)\
\r\n&#160;&#160;&#160;&#160;TeSt(q);&#160;//&#160;Here&#160;are&#160;q.x&#160;is&#160;setted&#160;to&#160;a&#160;integer&#160;456&#160;-&#160;right,&#160;but&#160;through&#160;TeSt...\
\r\n&#160;&#160;&#160;&#160;::print(\">After&#160;TeSt(q):&#160;q.x&#160;=&#160;\"&#160;+&#160;q.x&#160;+&#160;\"\\r\\n\");\
\r\n&#160;&#160;&#160;&#160;::print(\"FINISH\\r\\n\");\
\r\n";

int&#160;main(int&#160;argc,&#160;char*&#160;argv[])&#160;{
&#160;&#160;&#160;&#160;int&#160;&#160;&#160;&#160;&#160;i;
&#160;&#160;&#160;&#160;svm&#160;=&#160;sq_open(1024);&#160;//&#160;creates&#160;a&#160;VM&#160;with&#160;initial&#160;stack&#160;size&#160;1024&#160;
&#160;&#160;&#160;&#160;sq_setforeignptr(svm,&#160;0);
&#160;&#160;&#160;&#160;sqstd_seterrorhandlers(svm);&#160;//registers&#160;the&#160;default&#160;error&#160;handlers
&#160;&#160;&#160;&#160;sq_setprintfunc(svm,&#160;printfunc,&#160;errorfunc);&#160;//sets&#160;the&#160;print&#160;function
&#160;&#160;&#160;&#160;i&#160;=&#160;sizeof&#160;alf&#160;/&#160;sizeof&#160;*alf;
&#160;&#160;&#160;&#160;while(i&#160;--)
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SQ_register_global_func(svm,&#160;alf[i].function,&#160;alf[i].alias);
&#160;&#160;&#160;&#160;sq_pushroottable(svm);&#160;//push&#160;the&#160;root&#160;table(were&#160;the&#160;globals&#160;of&#160;the&#160;script&#160;will&#160;be&#160;stored)}
&#160;&#160;&#160;&#160;//
&#160;&#160;&#160;&#160;SQInteger&#160;oldtop&#160;=&#160;sq_gettop(svm);
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_compilebuffer(svm,&#160;szCmd,&#160;(int)strlen(szCmd)&#160;*&#160;sizeof&#160;SQChar,&#160;"compile",&#160;1);
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_pushroottable(svm);
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_call(svm,&#160;1,&#160;SQFalse,&#160;SQTrue);
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;sq_settop(svm,&#160;oldtop);
&#160;&#160;&#160;&#160;//
&#160;&#160;&#160;&#160;return&#160;0;
}

In the console that is prints:
Code:
BEGIN
Initial status: q.x = (null : 0x00000000)
>After Test(q): q.x = (null : 0x00000000)
>After TeSt(q): q.x = 456
FINISH
Press any key to continue
when I'm expecting for
Code:
BEGIN
Initial status: q.x = (null : 0x00000000)
>After Test(q): q.x = 123
>After TeSt(q): q.x = 456
FINISH
Press any key to continue

Many times for "googling" with different question set have no match for similar sample in the C-code.
Now this function is just very freak trash dump of different blind calls for many functions:

Can You help me for illuminate the one right way to solve this problem?
What is I'm must to know in this cases?

Thank You!
VoidSpirit
#2 Posted : Thursday, March 9, 2017 8:11:13 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/27/2016(UTC)
Posts: 7

Thanks: 0 times
Was thanked: 2 time(s) in 2 post(s)
You need to push 'self' into the stack before setting '123' value. Self is a table found in arguments.
But easy way is using Squirrel's internal classes methods: see stack_get, _table, SQTable::Set. For example
SQObjectPtr & p=stack_get(v,2); // take reference to the first argument
if(p._type==SQ_TABLE) _table(p)->NewSlot(SQString::Create(v,"x",-1),123); // set or create slot with '123' value
Alikberov
#3 Posted : Monday, March 13, 2017 6:55:45 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/1/2017(UTC)
Posts: 5
Location: Asia/Uzbekistan/Tashkent

Thanks: 1 times
Was thanked: 0 time(s) in 0 post(s)
VoidSpirit wrote:
You need to push 'self' into the stack before setting '123' value. Self is a table found in arguments.
But easy way is using Squirrel's internal classes methods

Thanks! But in my case it is a wrong idea.
The developed application conception are including a special synchronizing function, that take access to more resources. User-side script can forming an structure with unical members.
When members isn't nulled, requested resource will changed (writing direction).
When members is nulled, it's will filled by requested resource data.
As example, here an structure:
Code:
local      sync = {
            UPS = {
                  vac = null            // IN: Get the UPS-voltage
            },
            Console = {
                  LED = {
                        NumLock = true,// OUT: Toggle-on the indicator
                        CapsLock = null// IN: Read the indicator state
                  },
                  KeyStroke = null      // IN: Read keyboard string
            },
            Mouse = {
                  x = 0,            // OUT: Set X-axis of mouse
                  y = null            // IN: Read Y-axis of mouse
            }
      }

In the structure processing all members marked null will sets by requested resources. Others - will putted to this resources. So is just complex process of parsing table-tree.

P.S.: After lots experiments I'm has reached wished.
Just I'm wrote "sq_set(v, -9)" and all becomed correct.
VoidSpirit
#4 Posted : Tuesday, March 14, 2017 11:36:55 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/27/2016(UTC)
Posts: 7

Thanks: 0 times
Was thanked: 2 time(s) in 2 post(s)
Quote:
But in my case it is a wrong idea

Why? My sample is equal to your code but not uses a stack operations and stack indexing, only operate values directly (internally sq_newslot uses SQVM::NewSlot etc)

Quote:
Just I'm wrote "sq_set(v, -9)" and all becomed correct

Dangerous way I think - more complicated code, more error probability. You can check member existing by SQVM::Get (it's universal for all object types) and set values by SQVM::Set/NewSlot. It is very simply and reliably. Or use SQArray, SQTable, SQInstance own members
Alikberov
#5 Posted : Thursday, March 16, 2017 1:33:46 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/1/2017(UTC)
Posts: 5
Location: Asia/Uzbekistan/Tashkent

Thanks: 1 times
Was thanked: 0 time(s) in 0 post(s)
VoidSpirit wrote:
Why? My sample is equal to your code but not uses a stack operations and stack indexing, only operate values directly (internally sq_newslot uses SQVM::NewSlot etc)
Hm-mm... OK, I really is very bad know of all VM organization. You're is right.
VoidSpirit wrote:
Dangerous way I think - more complicated code, more error probability. You can check member existing by SQVM::Get (it's universal for all object types) and set values by SQVM::Set/NewSlot. It is very simply and reliably. Or use SQArray, SQTable, SQInstance own members
A problem I looking at the compiling process...
The compiler still bring lots of errors, like this:
Code:
error C2065: 'stack_get' : undeclared identifier
error C2440: 'initializing' : cannot convert from 'int' to 'struct SQObjectPtr &' A reference that is not to 'const' cannot be bound to a non-lvalue
error C2065: 'SQ_TABLE' : undeclared identifier
error C2027: use of undefined type 'SQTable'
\squirrel.h(95) : see declaration of 'SQTable'
error C2227: left of '->NewSlot' must point to class/struct/union
error C2027: use of undefined type 'SQString'
\squirrel.h(97) : see declaration of 'SQString'
(36) : error C2065: 'Create' : undeclared identifier

And I looking many problems with this basicaly, requirings lots of nerves and patience for overcoming. Just too smallest experiences for C++. And obsolute knowledge.
I feeling, "sq_set(v, -9)" - is illegal. But it's work. Why?

P.S.: Now I changed my "sq_set(v, -9)" to Your equivalency "sq_newslot(v, -6, false)"…
And this still work. Why?
What is the -9 and -6 values? Why only this values work properly?
I'm finally confused.
(Early I trying to user AngelScript, but some criteries is disliked: Shifted squirrel-syntax is suitable by me than classical C, in my case)

Thank You so much!
VoidSpirit
#6 Posted : Friday, March 17, 2017 7:48:29 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/27/2016(UTC)
Posts: 7

Thanks: 0 times
Was thanked: 2 time(s) in 2 post(s)
Sorry, some mistakes in my sample code...
1. For using squirrel classes your need include headers as "sqclass.h", "sqvm.h", "sqtable", "sqstring.h" etc (placed in "squirrel" subfolder).
2. Not SQ_TABLE constant - it is OT_TABLE, I wrote wrong.
Quote:
I feeling, "sq_set(v, -9)" - is illegal

It is legal completely, but a variant I wrote is more simple and has not stack operations (it uses Squirrel objects directly). And in your
example you're using a relative index -9 - this code is more complicated for writing and debugging I think.
Quote:
What is the -9 and -6 values? Why only this values work properly?

-9 - it is a relative stack index of 'self' for calling "_set" method in your example. Second parameter of "sq_set" is an index of "self" in the stack.
You have placed some values into the stack in your code, and a table is at the stack slot 9 from stack top. But it is uncomfortably because you
need change -9 value if you'll change your code.

Finally, compare two examples:
1. Normal using of squirrel functions for set value into the table slot
Code:
sq_pushroottable(v); // push 'self' into the stack - in this example it is root table for squirrel
sq_pushstring(v,"field",-1); // push name of slot into the stack
sq_pushinteger(v,123); // push value into the stack
sq_newslot(v,-3,SQFalse); // new slot for table placed in first line of this example (-3 - table, -2 - "field", -1 - "123").
sq_pop(v,1); // pop a "self" from stack

In first example values are pushed to stack. It is redundant operation.
2. Using squirrel methods
Code:
_table(v->_roottable)->NewSlot(SQString::Create(_ss(v),"field",-1),123);

No stack operations. Only set a value.
1 user thanked VoidSpirit for this useful post.
Alikberov on 3/18/2017(UTC)
absence
#7 Posted : Friday, March 17, 2017 6:11:43 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 109
Man
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 1 times
Was thanked: 10 time(s) in 10 post(s)
May I point out that accessing Sqirrel internals instead of using its API usually is generally a bad approach and should NOT be taught. It's prone to incompatibilities and issues, plus there are only really, really rare cases where you would need that litte extra bit of performance boost it provides.
(For example in case of possibly upcoming substantial changes to squirrel internals or, even worse, if he ever comes to use our (still closed-source) libraries, he'll be pretty much badly f*cked, for the latter even in the WORST way that can happen to any developer: unreproduceable running into memory and multithreading issues at some point late)

Do optimize proven bottlenecks of your application, but don't optimize things ahead simply for the sake of optimization. 99,9% a bad tradeoff.


(BTW, I didn't fully understand what this thread is about basically?)
Alikberov
#8 Posted : Saturday, March 18, 2017 12:15:09 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/1/2017(UTC)
Posts: 5
Location: Asia/Uzbekistan/Tashkent

Thanks: 1 times
Was thanked: 0 time(s) in 0 post(s)
VoidSpirit wrote:
Sorry, some mistakes in my sample code...
1. For using squirrel classes your need include headers as "sqclass.h", "sqvm.h", "sqtable", "sqstring.h" etc (placed in "squirrel" subfolder).
2. Not SQ_TABLE constant - it is OT_TABLE, I wrote wrong.
No, I long ago corrected this and find all necessary includings.
But, the <squirrel.h,sqstdio.h,sqstdaux.h> suggest for simplification of base project, I think.
And sq_*-functions is good encapsulating the name-space of VM, than naked Squirrel, when I needed for new separate cpp-file in my common project. And does not be fit in the "quickly" project.
VoidSpirit wrote:

-9 - it is a relative stack index of 'self' for calling "_set" method in your example. Second parameter of "sq_set" is an index of "self" in the stack.
I understand this. But the -9 - is "too deeply" level and I can't understand, why I fell at there. :)
VoidSpirit wrote:
Finally, compare two examples:
Yes.
But, I until it is ready for build separate enveloped sub-library. And I wish to probe my-self conception in whole. May be it's bad.
(Like the vvvv, my idea is work with up to 10 VirtualDub in real-time, and Squirrel-script, uses OpenGL-basic primitives, repainted streams direct into one master. Like a "Virtual Studio". But very raw, buggest and lamed.
Because it is very difficult to project my-self original idea from blank and regardless of all official. Interest sake.)

P.S.: Anyway, thank You for advice. ;)
Users browsing this topic
Guest (2)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.255 seconds.