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

I can't run sample code, return JsErrorNoCurrentContext #1799

Closed
fettliu opened this issue Oct 21, 2016 · 11 comments
Closed

I can't run sample code, return JsErrorNoCurrentContext #1799

fettliu opened this issue Oct 21, 2016 · 11 comments

Comments

@fettliu
Copy link

fettliu commented Oct 21, 2016

this file in "@ubuntu:~/github/ChakraCore/BuildLinux/Release/bin/ChakraCore"


#include "../../../../lib/Jsrt/ChakraCore.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <locale>
#include <codecvt>

using namespace std;



const std::wstring utf82ws(const std::string& src)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t> > conv;
    return conv.from_bytes(src);
}

int print_err()
{
    JsValueRef err;
    if(JsNoError == JsGetAndClearException(&err))
    {
        JsValueRef errstr;
        if(JsNoError == JsConvertValueToString(err, &errstr))
        {
            char* output1;
            size_t outputLength;
            if(JsNoError == JsStringToPointerUtf8Copy(errstr, &output1, &outputLength))
            {
                wcout << utf82ws(output1) << endl;
                printf(output1);
            }
        }
    }
    return 1;

}

int main()
{
    JsRuntimeHandle runtime;
    JsContextRef context;
    JsValueRef result;
    unsigned currentSourceContext = 0;

    // Your script; try replace hello-world with something else
    string script = "(function(){return \'Hello world!\';})()";

    // Create a runtime. 
    if(JsNoError != JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime))
        return print_err();

    // Create an execution context. 
    if(JsNoError != JsCreateContext(runtime, &context))
        return print_err();

    // Now set the current execution context.
    if(JsNoError != JsSetCurrentContext(context))
        return print_err();

    // Run the script.
    auto ec = JsRunScriptUtf8(script.c_str(), currentSourceContext++, "", &result);
    if(ec != JsErrorCode::JsNoError)
    {
        print_err();
        return 1;
    }

    JsValueRef resultJSString;
    JsConvertValueToString(result, &resultJSString);
    if(ec != JsErrorCode::JsNoError)
    {
        print_err();
        return 1;
    }

    // Project script result back to C++.
    char* output;
    size_t outputLength;
    JsStringToPointerUtf8Copy(resultJSString, &output, &outputLength);

    string resultW(output);
    //cout << string(resultW.begin(), resultW.end()) << endl;
    printf(output);
    system("pause");

    // Dispose runtime
    JsSetCurrentContext(JS_INVALID_REFERENCE);
    JsDisposeRuntime(runtime);

return 0;
}


at JsRunScriptUtf8
return JsErrorNoCurrentContext

btw: pls support centos7.

@fettliu
Copy link
Author

fettliu commented Oct 21, 2016

The same code run successful at vs2015, only cut off Utf8 of JsXxxxxUtf8 and convert string to wstring or wchar_t.

@obastemur
Copy link
Collaborator

obastemur commented Oct 21, 2016

btw: pls support centos7.

ChakraCore runs on Fedora. Should be running on centos too.

@obastemur
Copy link
Collaborator

@FettLuo static or dynamic lib? could you please share the steps in detail?

@fettliu
Copy link
Author

fettliu commented Oct 21, 2016

@obastemur ubuntu server 16.10 on HyperV on my SurfacePro3. ChakraCore is dynamic lib(so).
build use clang++ test.cpp -std=c++11 -g -lChakraCore.

@obastemur
Copy link
Collaborator

@fettliu
Copy link
Author

fettliu commented Oct 22, 2016

  1. I use c++, not python. :( , I don't known how to call DllMain in c++ in linux.
  2. Not call DllMain in Linux_OSX sample in c++.

BTW:Manual call DllMain that not enough smart in linux. best auto call DllMain at first call CreateRuntime internal.

@obastemur
Copy link
Collaborator

@FettLuo why don't you use static library on xplat?

@fettliu
Copy link
Author

fettliu commented Oct 23, 2016

@obastemur
I don't know much about ChakraCore static libraries used, intuition is a dynamic library simpler.
Also, I hope my main program in Windows and Linux have the same characteristics, that is dynamically loaded jsrt.

Documentation on using statically linked libraries?

@obastemur
Copy link
Collaborator

@FettLuo I agree the DllMain call requirement is ugly.

Opened an issue (#1811) to track that.

Static library usage sample:

https://github.com/Microsoft/Chakra-Samples/tree/master/ChakraCore%20Samples/Hello%20World/Linux_OSX

@fettliu
Copy link
Author

fettliu commented Oct 24, 2016

Hope can be directly compiled in Centos. CentOS has a lot of users in China.
Want to extract the header files needed by the third party to a separate Directory, do not include the source code.

@fettliu fettliu closed this as completed Oct 24, 2016
@obastemur
Copy link
Collaborator

Hope can be directly compiled in Centos.

See #1312 for Fedora steps. Shouldn't be so different.

@liminzhu , what do you think about adding those on docs ?

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