在iOS中使用JSContextGroupSetExecutionTimeLimit [英] Using JSContextGroupSetExecutionTimeLimit in iOS

查看:154
本文介绍了在iOS中使用JSContextGroupSetExecutionTimeLimit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在JSContext对象中停止执行Javascript吗?我发现以下方法正是我所需要的

I need to stop the execution of Javascript in a JSContext object? I found the following method which exactly what I need

@function
@abstract Sets the script execution time limit.
@param group The JavaScript context group that this time limit applies to.
@param limit The time limit of allowed script execution time in seconds.
@param callback The callback function that will be invoked when the time limit
 has been reached. This will give you a chance to decide if you want to
 terminate the script or not. If you pass a NULL callback, the script will be
 terminated unconditionally when the time limit has been reached.
@param context User data that you can provide to be passed back to you
 in your callback.

 In order to guarantee that the execution time limit will take effect, you will
 need to call JSContextGroupSetExecutionTimeLimit before you start executing
 any scripts.
*/
JS_EXPORT void JSContextGroupSetExecutionTimeLimit(JSContextGroupRef, double limit, JSShouldTerminateCallback, void* context) AVAILABLE_IN_WEBKIT_VERSION_4_0;

不幸的是,该方法没有很好的记录,我无法弄清楚如何使用它。

Unfortunately the method is not well documented and I can't figure out how I can use it.

我的代码是

JSContextGroupRef group = JSContextGroupCreate();
JSGlobalContextRef context = JSGlobalContextCreateInGroup(group, NULL);
NSString *code = @"while(1){}";
JSStringRef script = JSStringCreateWithCFString((__bridge CFStringRef)code);
JSContextGroupSetExecutionTimeLimit(group,.200f,nil,0);
JSValueRef error = NULL;
JSValueRef value = JSEvaluateScript(context, script, NULL, NULL, 0, &error);

当我尝试调用JSContextGroupSetExecutionTimeLimit时出现编译错误。

when I try to to call to JSContextGroupSetExecutionTimeLimit I get compilation error.


函数的隐式声明JSContextGroupSetExecutionTimeLimit
在C99中无效,

Implicit declaration of function "JSContextGroupSetExecutionTimeLimit" is invalid in C99,

似乎调用来自JSContextRefPrivate.h,我可以将这个标题添加到我的项目以及实现的位置。

seems like the call comes from JSContextRefPrivate.h, the question how I can add this header to my project and where is the implementation.

任何建议如何处理这个问题。谢谢

any advice how can I handle this. thanks

推荐答案

这实际上非常简单:只需复制相关函数的声明(以及 JSShouldTerminateCallback typedef)到您的源代码中。这告诉编译器在运行时可以使用这些函数的确切签名。

It's actually pretty trivial: Just copy the declarations of the relevant functions (and the JSShouldTerminateCallback typedef) into your source code. This tells the compiler the exact signatures with which the functions will be available at runtime.

由于JavascriptCore是开源WebKit项目的一部分,函数声明即使是 WebKit存储库中的有用说明。相关部分是:

As JavascriptCore is part of the open source WebKit project, the function declarations are available even with useful descriptions in the WebKit repository. The relevant parts are:

typedef bool
(*JSShouldTerminateCallback) (JSContextRef ctx, void* context);

JS_EXPORT void JSContextGroupSetExecutionTimeLimit(JSContextGroupRef group,
                                                   double limit, JSShouldTerminateCallback callback, void* context) CF_AVAILABLE(10_6, 7_0);

JS_EXPORT void JSContextGroupClearExecutionTimeLimit(JSContextGroupRef group) CF_AVAILABLE(10_6, 7_0);

此外,您不必自己创建一个组,因为根据 JavascriptCore文档,您创建的每个JSContextRef都会分配一个新的JSContextGroup。您可以使用公共 JSContextGetGroup 获取上下文组函数。

Also, you don't have to create a group yourself, as according to the JavascriptCore documentation, every JSContextRef you create is assigned a new JSContextGroup. You can get the group of a context using the public JSContextGetGroup function.

使用私有 JSContextGroupSetExecutionTimeLimit API的示例可在 WebKit测试

An example using the private JSContextGroupSetExecutionTimeLimit API is available in the WebKit tests.

重要提示:正如已经指出的那样,使用此私有API的应用可能会从App Store中被拒绝。

这篇关于在iOS中使用JSContextGroupSetExecutionTimeLimit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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