-
Notifications
You must be signed in to change notification settings - Fork 85
Conversation
Thanks, this sample worked for me on Ubuntu 14.04. Unfortunately, as I mentioned in (chakra-core/ChakraCore#1353) I believe there is a serious flaw to this approach. Any JsValueRef will not be kept alive on the C stack, so if a GC occurs they will end up getting collected prematurely. For example, if you change:
to
This will cause the sample to segfault as jsResult gets garbage collected since there are no references to it on the C stack. I'm not sure if there is any way of avoiding this, other than doing something like wrapping every API function in a C function that calls JsAddRef on the returned JsValueRef (and then calling JsRelease from Python when you are done with the JsValueRef). |
Or it could be potentially be done with some creative abuse of CFUNCTYPE from Python ctypes. I'll maybe give that a go when I get a chance. |
I'm not sure if I understood what you mean by C reference to a pointer and GC? |
@cosinusoidally Are you mixing C variables to C++ class destruction / scope ? If you keep a reference to (in the JS land not C) to that return value, it wouldn't be GC'ed. |
@obastemur can you produce a readme stating the purpose and requirement for the sample similar to what we have for other hello-world samples? |
Sorry I wasn't clear. I was referring to the CharkraCore GC doing conservative stack scanning to locate references to JavaScript objects from the native C stack (https://github.com/Microsoft/ChakraCore/wiki/JavaScript-Runtime-%28JSRT%29-Overview#memory-management). These references are just bare pointers to JsValueRefs. In the case of the Python sample, these pointers will live on the heap, and so will not be seen by the GC. For example, if you apply the following patch to the C++ sample (https://github.com/Microsoft/Chakra-Samples/blob/master/ChakraCore%20Samples/Hello%20World/Linux_OSX/sample.cpp) you will note that it segfaults:
This crashes because I have overwritten If you then run the sample under gdb, you will observe that the crash goes away if you write the contents of
As you mention, this can sometimes be worked around by making sure there is a reference to the Garbage Collectable thing from JavaScript land (eg in this case by changing the |
@cosinusoidally I agree and don't agree. Python's C interface has its own issues/flavor yet that doesn't defeat the purpose or how ChakraCore can be reached from Python's C or how shared library needs to be initialized. I'm unable to see the difference with the sample you provided and the Python case you have pointed out earlier. On both you trigger the conservative stack powered GC hence You actually do the same thing (as There are multiple ways to overcome those based on which platform you are embedding ChakraCore. i.e. JsAddRef -> https://msdn.microsoft.com/en-us/library/dn249659(v=vs.94).aspx This has been a valuable conversation. Thanks for that. |
@liminzhu review please. @cosinusoidally Thanks for the remarks. |
|
||
# You are expected to initialize the DllMain manually | ||
# Attach process | ||
chakraCore.DllMain(0, 1, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are expected to initialize the DllMain manually
Can you elaborate more here? Trying the sample on Windows with py 2.7.5 and getting AttributeError: function 'DllMain' not found
on Line 13. Googling doesn't really help me unfortunately.
Depends on what's the issue here, adding some content in readme may be helpful.
Added some nitpicks in README 😄 |
@liminzhu thanks for the review and comments. Updated both sample and readme. |
stringLength = c_size_t(); | ||
chakraCore.JsStringToPointerUtf8Copy(resultJSString, byref(resultSTR), byref(stringLength)); | ||
|
||
print("Result from ChakraCore: ", resultSTR.value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python does this weird thing that print is changed from a statement to a function from Python 2 to 3, so this line is going to print something like ('Result from ChakraCore: ', 'Hello world!')
in Python 2.x. Something like below will work better,
print("Result from ChakraCore: %s" % resultSTR.value);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python 2.7+ doesn't cover Python 3. Python 3 is another world :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should change it then. It prints that weird-looking tuple in 2.7 :p.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
upps. :)
Added Linux/Windows requirements. |
No description provided.