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

How to return other foreign class obj from a foreign class ? #1182

Open
tommego opened this issue Nov 25, 2023 · 7 comments
Open

How to return other foreign class obj from a foreign class ? #1182

tommego opened this issue Nov 25, 2023 · 7 comments

Comments

@tommego
Copy link

tommego commented Nov 25, 2023

I'm Writing small game engine with wren binding support(love wren language very mutch, binding raylib to wren...)
My wren.inc codes:

foreign class Color{
    foreign r
    foreign r=(val)
    foreign g
    foreign g=(val)
    foreign b
    foreign b=(val)
    foreign a
    foreign a=(val)
}

foreign class Object {
    foreign color // but how to implement this form c/c++ using wrenSetSlotNewForeign? this function I tested it's classSlot just must be 0, and only could create new instance object to class Object 
    foreign color=(val) // currently I can implement this from c/c++
}

My cpp codes:(for getter/setter)

void wrenSetObjectColor(WrenVM* vm) {
    ((Object*)wrenGetSlotForeign(vm, 0))->color =  *((Color*)wrenGetSlotForeign(vm, 1));
}

// but how to implement?
void wrenGetObjectColor(WrenVM* vm) {
   auto inst =  ((Object*)wrenGetSlotForeign(vm, 0));
   wrenSetSlotNewForeign(vm, 0, ?, sizeof(Color));
   auto tmp = ((Color*)wrenGetSlotForeign(vm, 0));
   memcpy(tmp, &inst->color, sizeof(Color));
}
// this not work...

I had implement Json.hpp Bingding for wren Map value, my codes post to this issue.
Json.hpp binding to wren Map codes & test

@tommego
Copy link
Author

tommego commented Nov 25, 2023

I Known I can use Handle to store on setter implement, and return handle to getter implement, but some times those foreign objects are initialized on c/c++code, and without setting property action, and that time it will return null value.

@tommego
Copy link
Author

tommego commented Nov 25, 2023

image
Just like MatrixToFloatV function, it should return a foreign classe value named Float16,but there is no way to create Float16 foreign instance for Matrix's 0 slot.

@mhermier
Copy link
Contributor

mhermier commented Nov 25, 2023 via email

@tommego
Copy link
Author

tommego commented Nov 26, 2023

You need an intermediate function to create a Color instance, so you can initialize it with the real value: foreign class Object { color { color_(Color.new()) } foreign color_(color) }

Thanks for your answer, this way work well for getter/setter!

@tommego
Copy link
Author

tommego commented Nov 30, 2023

I actually need to implement my first post function at all..., but there is no way to create other foreign class object to slot 0 for my return value.

@mhermier
Copy link
Contributor

mhermier commented Nov 30, 2023 via email

@tommego
Copy link
Author

tommego commented Dec 7, 2023

Can you elaborate. I don't understand what you mean

I‘am writing node editor with full wren codes, binding with raylib graphic engine.
image
I found new way to bind c++ foreign class to wren scripting env directly, I'll post new messages and questions for the future!

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

2 participants