microsoft / Chakra-Samples

Repository for Chakra JavaScript engine related samples.
MIT License
216 stars 84 forks source link

How to pass .Net object to Javascript context #27

Closed noh626 closed 7 years ago

noh626 commented 8 years ago

Hi,

I just started a project what is to embed Chakracore into .Net application.

The main goal of application is to run realtime hosting. The application is to get javascript code from client side and process external objects with it.

The external object is supposed to be complicated .NET type.

But I have some difficulties in passing the external object into javascript context.

I tired below code and ran javascript code to see the test object exists.

But the result showed the object exits but no property for the object at all even thou the test object had data members.

JavaScriptValue global = JavaScriptValue.GlobalObject;

Test test = new Test(); GCHandle handle = GCHandle.Alloc(test); IntPtr ptr = (IntPtr)handle;

JavaScriptValue jsv = JavaScriptValue.CreateExternalObject(ptr, null); JavaScriptPropertyId propertyId = JavaScriptPropertyId.FromString("test"); global.SetProperty(propertyId, jsv, false);

Can anyone tell me how to implement this or give me any good example?

Thanks,

nakosung commented 8 years ago

Maybe https://github.com/ncsoft/unreal.js gives you a clue.

noh626 commented 8 years ago

@nakosung thanks!

liminzhu commented 7 years ago

Close due to inactivity.