GithubHelp home page GithubHelp logo

About Core::* types about objectscript HOT 6 OPEN

IngwiePhoenix avatar IngwiePhoenix commented on July 20, 2024
About Core::* types

from objectscript.

Comments (6)

unitpoint avatar unitpoint commented on July 20, 2024

You could obtain "function pointers" from the script using getValueId. BUT the function could be freed by OS garbage collector so you have to retain the function using retainValueById and release it (see releaseValueById) when you don't want to use the function.

You could put saved function to OS stack using pushValueById.

You could make value as permanent using retainValueById.

You could obtain other types as permanent values using the same way. Numbers, booleans and null are simple types. They have no id so you should save them as is (see toNumber, toFloat, toBool and so on).

The Core class is used internally. There are a lot of low end operations inside of the core. So programmer have to use the core carefully, it could be not simple. I was trying to implement general API functions inside of OS class so you should not use the core usually. You could use core's types and functions if you know what you do.

Feel free to ask me any questions.

from objectscript.

IngwiePhoenix avatar IngwiePhoenix commented on July 20, 2024

:) https://github.com/IngwiePhoenix/ObjectScriptValue
I'm on it. This shouldn't be too hard. I just started it, like, half a hour ago...

The idea is to mimic the life cycle of variables. So when the instance goes out of scope, the associated value does too. (see destructor).

I was wondering how I could make function calling easy, to support types. But first I need to get objects and such to work for real. I have only tested things using test.cpp so far. If you have any suggestions while I am at it, go ahead! I'll try to make it as compliant as possible, so it can be put into any OS projects.

from objectscript.

IngwiePhoenix avatar IngwiePhoenix commented on July 20, 2024

Im stuck at the getter. I know how to get Object properties with strings:

                case OS_VALUE_TYPE_OBJECT:
                    myOS->pushValueById(valueID);
                    myOS->getProperty(index);
                    char* str = (char*)(myOS->toString().toChar());
                    myOS->pop();
                    return str;

But how do I access array elements with a numeric index? I.e.: arr[2]

from objectscript.

unitpoint avatar unitpoint commented on July 20, 2024

Awesome, you work very hard!

I've reviewed your OSValue.cpp, it seems like you need to upgrade OS version, there is no OS_VALUE_TYPE_UNKNOWN in ObjectScript at the moment. The latest version is ObjectScript 2.3.8-rc.

You could obtain a value from array as follows:

case OS_VALUE_TYPE_ARRAY:
    myOS->pushValueById(valueID);
    myOS->pushNumber(numIndex); // index of value
    /* Obtain string value from array... */
    myOS->getProperty();
    return myOS->popString();

The same way works for objects:

case OS_VALUE_TYPE_OBJECT:
case OS_VALUE_TYPE_USEDDATA:
case OS_VALUE_TYPE_USERPTR:
    myOS->pushValueById(valueID);
    myOS->pushString(strIndex); // string index of value
    // or myOS->pushNumber(numIndex); // number index of value
    myOS->getProperty();
    /* Obtain string value from object */
    return myOS->popString();
    // or obtain number value from object
    // return myOS->popNumber(); 

Note that OS's garbage collector frees unused values so you should retain (add a reference) value used by your own code or copy it. Be carefull using strings. You can use OS::String type as string value in your own code. OS::String retains string value already. Or copy string using std::string for example.

So your code is not safe

                char* str = (char*)(myOS->toString().toChar());
                myOS->pop();

change it to

                return myOS->popString();

or to

                return std::string((char*)(myOS->popString().toChar());

P.S.
OS_VALUE_TYPE_USEDDATA and OS_VALUE_TYPE_USEDPTR works like object but they created from C++ objects, for example DateTime is userdata really.

from objectscript.

IngwiePhoenix avatar IngwiePhoenix commented on July 20, 2024

Thanks! I will adapt that right in.

Yep, I work hard if I am focused on something. I am making this extension out of personal need for being able to store references to script functions later on.

In the constructor, I am already calling retainById in order to keep the object in cycle. On destruction, the value is freed accordingly.

My goal is to make an "almost complete" wrapper for the script types, so that its easier for C++ code to store them - like I need to store functions an dobjects; actually, the entire object contianing a function.

I was using a version of OS that I had downloaded at some point, will re-clone to have a properly updated header file. Thanks for the tipp. =)

from objectscript.

IngwiePhoenix avatar IngwiePhoenix commented on July 20, 2024

There goes. I now am going to implement numeric array access, accessing strings by number, etc. Shall be interesting! :)

After that, I think I can move on to setters. If an object has been declared (newObject, newArray), and then the Value instance was made, it should be able to also add values to the respective values. That is more like a little gimmick so I can later modify the object from within my code.

After that, I need to find a good way to implement calling functions, or just making a function to put the function into the stack...

But yeah, this little extension should be done soon! :)

from objectscript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.