JavaScriptCore console.log [英] JavaScriptCore console.log

查看:210
本文介绍了JavaScriptCore console.log的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整理了一个非常简单的程序,使用JavaScriptCore来评估JS:

I've put together a very simple program that uses JavaScriptCore to evaluate JS:

#import <CoreFoundation/CoreFoundation.h>
#import <JavaScriptCore/JavaScriptCore.h>

int main(int argc, const char * argv[])
{
    JSGlobalContextRef ctx = JSGlobalContextCreate(NULL);

    FILE *f = fopen(argv[1],"r");
    char * buffer = malloc(10000000);
    fread(buffer,1,10000000,f);

    CFStringRef strs = CFStringCreateWithCString(NULL, buffer, kCFStringEncodingASCII);

    JSStringRef jsstr = JSStringCreateWithCFString(strs);
    JSValueRef result = JSEvaluateScript(ctx, jsstr, NULL, NULL, 0, NULL);

    double res  = JSValueToNumber(ctx, result, NULL);
    JSGlobalContextRelease(ctx);

    printf("%lf\n", res);
    return 0;
}

这里的想法是最后一个值应该是数字,并打印该值。这适用于有效的JavaScript代码,例如

The idea here is that the last value is expected to be a Number, and that value is printed. This works for valid javascript code, such as

var square = function(x) { return x*x; }; square(4)

但是,如果代码尝试执行控制台。 log ,程序段错误。 JSC中是否有可用的日志功能,还是我必须自己滚动?

However, if the code tries to perform a console.log, the program segfaults. Is there a log function available in JSC or do I have to roll my own?

推荐答案

您必须提供自己的控制台如果使用Mac或IOS的JavaScriptCore框架,请记录。

You do have to provide your own console log if using the JavaScriptCore framework from Mac or IOS.

这里有一些对我有用的代码(对不起,根据你上面的代码,它是Objective-C而不是标准C ):

Here is some code that worked for me (sorry it is Objective-C rather than standard C as per your code above):

JSContext *javascriptContext  = [[JSContext alloc] init];
javascriptContext[@"consoleLog"] = ^(NSString *message) {
    NSLog(@"Javascript log: %@",message);
};

然后你从Javascript使用它:

Then you use it from Javascript by:

consoleLog("My debug message");

请注意,我试图定义一个vararg版本(记录多个参数)但我不能让它在框架api中正常工作。

Note that I have tried to define a vararg version (log taking multiple parameters) but I couldn't get this to work correctly across the framework api.

请注意,此解决方案使用随新的Objective-C API引入的功能,同时为JavaScriptCore.framework引入IOS 7.如果您正在寻找Objective-C和Javascript之间这个良好集成的桥梁的介绍,请查看Apple开发者网络上的2013 WWDC简介将JavaScript集成到本机应用程序会话:https://developer.apple.com/videos/wwdc/2013/?id=615

Note that this solution uses features introduced with the new Objective-C API for the JavaScriptCore.framework introduced at the same time as IOS 7. If you are looking for an intro to this well-integrated bridge between Objective-C and Javascript, check out the 2013 WWDC introduction "Integrating JavaScript into Native Apps" session on Apple's developer network: https://developer.apple.com/videos/wwdc/2013/?id=615

更新回答:

对于那些想要在没有重构的情况下最大限度地重复使用javascript代码的人,我设法让版本正常工作声明 console.log()形式的日志:

For those of you wanting to maximise your javascript code reuse without refactoring, I've managed to get a version working that declares a log of the form console.log() :

JSContext *javascriptContext  = [[JSContext alloc] init];
[javascriptContext evaluateScript:@"var console = {}"];
javascriptContext[@"console"][@"log"] = ^(NSString *message) {
    NSLog(@"Javascript log: %@",message);
};

然后你从Javascript使用它:

Then you use it from Javascript by:

console.log("My debug message");

这篇关于JavaScriptCore console.log的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆