wren-lang / wren

The Wren Programming Language. Wren is a small, fast, class-based concurrent scripting language.
http://wren.io
MIT License
6.83k stars 545 forks source link

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

Open tommego opened 8 months ago

tommego commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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)

}

tommego commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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

tommego commented 7 months ago

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!