Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with nesting classes #24

Open
HassanEmam opened this issue Oct 12, 2022 · 7 comments
Open

Issues with nesting classes #24

HassanEmam opened this issue Oct 12, 2022 · 7 comments

Comments

@HassanEmam
Copy link

I have created a library that parses a tsv file. Each line parsed is added to an object of another class that is used as a collections.

When I used the template to compile I had some initial issues accessing the filesystem but that has been sorted by modifying the CMAKELists and adding some extra arguments.

Now all compiles ok and I have exposed the functions and members using Glue; I am faced a new issue that I can access a member but when I try to console log the data in that member it does not recognise it.

The code for the library is https://github.com/Constology/XERNative

Any suggesting on what is the best practice to write the Glue code for this library?

@TheLartians
Copy link
Owner

TheLartians commented Oct 12, 2022

Hey thanks for the issue! If I understand correctly, you want be be able to console.log a C++ object representing your CSV?
In that case you can set the glue::keys::operators::tostring property of the Glue class to a function that returns the string value for that object.

Unfortunately I don't have the time to write a proper documentation, besides the Glue readme and EmGlue readme, but you can find an example in the unit tests.

@FireBlaze236
Copy link

Hello, I am working on the same problem as here.
Suppose we have a class called,

class Writer{
public:
    float prop = 1.02f;
};

Glued like follows:

lib["Writer"] = glue:createClass<Writer>().addConstructor().addMember("prop", &Writer::prop);
console.log(readerModule.prop());

prints the floating point number.

Now if we had:

class Writer{
public:
    LibaryClass prop;
};

And then.

console.log(readerModule.prop());

This prints Any{}. So, my question is how to actually create the bindings for these classes. Do I have to manually create all the bindings? Or is there a preferred way of creating those mappings.

@HassanEmam
Copy link
Author

The glue code with the template is published here

https://github.com/Constology/XERNative/tree/wasm

@TheLartians
Copy link
Owner

TheLartians commented Oct 19, 2022

This prints Any{}. So, my question is how to actually create the bindings for these classes. Do I have to manually create all the bindings? Or is there a preferred way of creating those mappings.

Yes, you would need to add a definition somewhere for all types that you want to interact with from JavaScript. So in your case, to be able to console.log the LibraryClass, you would need to add something like

lib["Writer"] = glue:createClass<Writer>().addConstructor().addMember("prop", &Writer::prop);

lib["LibaryClass"] = glue:createClass<LibaryClass>().addMember(
  glue::keys::operators::tostring,
  [](const LibaryClass &obj){
    return <code to convert LibaryClass to a string>;
  }
);

@TheLartians
Copy link
Owner

@HassanEmam @FireBlaze236 did this help with your issue?

@HassanEmam
Copy link
Author

@TheLartians thanks for your response. I am still struggling to be honest.

I have a Reader class that parses a file which contains Tasks class that has a vector of class Task

The Task class has a lot of properties. what I want to achieve is to return an array of tasks with access to the properties not only a string.

@TheLartians
Copy link
Owner

If you define the array type e.g. using glue::createArrayClass<std::vector<int>>() (see here for usage test-case) and add it to your lib object, you should be able to return it from a function and access it from TypeScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants