gpt4 book ai didi

javascript - 在 iOS 中使用 JSContextGroupSetExecutionTimeLimit

转载 作者:行者123 更新时间:2023-11-29 15:25:24 24 4
gpt4 key购买 nike

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

@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;

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

我的代码是

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 时出现编译错误。

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

似乎调用来自 JSContextRefPrivate.h,问题是我如何将此 header 添加到我的项目以及实现在哪里。

任何建议我该如何处理。谢谢

最佳答案

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

由于 JavascriptCore 是开源 WebKit 项目的一部分,即使在 WebKit repository 中有有用的描述,函数声明也可用。 .相关部分是:

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 documentation,您不必自己创建群组。 ,您创建的每个 JSContextRef 都会分配一个新的 JSContextGroup。您可以使用公共(public) JSContextGetGroup 获取上下文组功能。

使用私有(private) JSContextGroupSetExecutionTimeLimit API 的示例在 WebKit tests 中可用.

重要提示:如前所述,使用此私有(private) API 的应用很可能会被 App Store 拒绝。

关于javascript - 在 iOS 中使用 JSContextGroupSetExecutionTimeLimit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39675926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com