#include "objectscript.h" using namespace ObjectScript; int main() { OS * os = OS::create(); // TODO: main code here os->release(); return 0; }
delete
, but with the call of the release
method. print("10 * (3+2) = ", 10 * (3+2))
this
for this function. If the function does not use this, well, for example, a static function, then null
should be placed as this
. We do it like this: os->getGlobal("print"); // #1 - os->pushNull(); // #2
os->pushString("10 * (3+2) = "); // #3 -
os->pushNumber(10); // #4 os->pushNumber(3); // #5 os->pushNumber(2); // #6 os->runOp(OP_ADD); // #5 - 3+2 os->runOp(OP_MUL); // #4 - 10 * (3+2)
runOp
method can perform mathematical, logical, and bitwise operators on values in the stack using the OS kernel. In other words, if necessary, type conversions will occur, and so on ... OP_ADD
performs an addition operator on two values at the top of the stack (that is, what was placed on the stack the last two times). The result will replace them in the stack (that is, two values will be removed from the stack, and the result added). OP_MUL
- likewise for multiplication. os->call(2); // 2
print
print the result to the console), it should be like this: 10 * (3+2) = 50
bar = {firsname="James", lastname="Bond"} bar.profession = "actor" print bar
os->newObject(); // #1
firsname="James"
: os->pushStackValue(-1); // #2
os->pushString("firsname"); // #3 - os->pushString("James"); // #4 - os->setProperty(); // #1
setProperty
method sets a property and removes the values used from the stack (in this case, three values are used at the top of the stack: object, property name and value). os->pushString("Bond"); // #2 - os->setProperty(-2, "lastname"); // #1
"Bond"
. os->setGlobal("bar"); // #0
bar.profession = "actor"
: os->getGlobal("bar"); // #1 - bar os->pushString("actor"); // #2 os->setProperty(-2, "profession"); // #1 os->pop(); // #0 - bar
print bar
: os->getGlobal("print"); // #1 os->pushNull(); // #2 os->getGlobal("bar"); // #3 os->call(1); // #0
{"firsname":"James","lastname":"Bond","profession":"actor"}
print(concat(5, " big differences"))
os->getGlobal("print"); // #1 - print os->pushNull(); // #2 - this print os->getGlobal("concat"); // #3 - concat os->pushNull(); // #4 - this concat os->pushNumber(5); // #5 - concat os->pushString(" big differences"); // #6 - concat os->call(2, 1); // #3 - concat
print
: os->call(1); // #0
5 big differences
#include "objectscript.h" using namespace ObjectScript; int main() { OS * os = OS::create(); /* print("10 * (3+2) = ", 10 * (3+2)) */ os->getGlobal("print"); // #1 - stack values, it's print function from standart library os->pushNull(); // #2 - null, it's function this, each call of function must have this // push the first argument os->pushString("10 * (3+2) = "); // #3 - we have 3 stack values here // prepare second argument os->pushNumber(10); // #4 os->pushNumber(3); // #5 os->pushNumber(2); // #6 os->runOp(OP_ADD); // #5 - 3+2 os->runOp(OP_MUL); // #4 - 10 * (3+2) os->call(2); // call function with 2 arguments /* bar = {firsname="James", lastname="Bond"} bar.profession = "actor" print bar */ os->newObject(); // #1 - new object os->pushStackValue(-1); // #2 - the same object, -1 - is relative pointer to the top stack value os->pushString("firsname"); // #3 - property key os->pushString("James"); // #4 - property value os->setProperty(); // #1 - setProperty uses 3 stack values and pop them // second way of same functionality os->pushString("Bond"); // #2 - property value os->setProperty(-2, "lastname"); // #1 os->setGlobal("bar"); // #0 - assign object value to global bar variable, pop value // let's do bar.profession = "actor" os->getGlobal("bar"); // #1 - our global a variable os->pushString("actor"); // #2 - property value os->setProperty(-2, "profession"); // #1 os->pop(); // #0 // let's do print bar os->getGlobal("print"); // #1 os->pushNull(); // #2 os->getGlobal("bar"); // #3 os->call(1); // #0 /* print(concat(5, " big differences")) */ os->getGlobal("print"); // #1 - print function os->pushNull(); // #2 - this for print os->getGlobal("concat"); // #3 - concat function os->pushNull(); // #4 - this for concat os->pushNumber(5); // #5 os->pushString(" big differences"); // #6 os->call(2, 1); // #3 - result is already at the top of stack os->call(1); // #0 os->release(); return 0; }
Source: https://habr.com/ru/post/152797/
All Articles