JavaScriptCore console.log

编程入门 行业动态 更新时间:2024-10-12 05:50:57
本文介绍了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集成到本机应用程序会话:developer.apple/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: developer.apple/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

本文发布于:2023-11-27 18:24:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639099.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JavaScriptCore   console   log

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!