Permalink
Browse files

timers, feat: support script timeout.

  • Loading branch information...
xicilion committed Jul 27, 2018
1 parent 78a312a commit 973270c8e25252bea0033813754d7503bf456234
Showing with 105 additions and 1 deletion.
  1. +2 −0 fibjs/include/Fiber.h
  2. +22 −1 fibjs/include/ifs/timers.h
  3. +3 −0 fibjs/src/coroutine/Fiber.cpp
  4. +70 −0 fibjs/src/global/timeout.cpp
  5. +8 −0 idl/zh-cn/timers.idl
View
@@ -40,6 +40,7 @@ class JSFiber : public Fiber_base,
: m_native_name(NULL)
, m_c_entry_fp_(NULL)
, m_handler_(NULL)
, m_termed(false)
{
m_id = holder()->m_fid++;
}
@@ -137,6 +138,7 @@ class JSFiber : public Fiber_base,
const char* m_native_name;
void* m_c_entry_fp_;
void* m_handler_;
bool m_termed;
private:
int64_t m_id;
View
@@ -31,6 +31,7 @@ class timers_base : public object_base {
static result_t clearHrInterval(v8::Local<v8::Value> t);
static result_t setImmediate(v8::Local<v8::Function> callback, OptArgs args, obj_ptr<Timer_base>& retVal);
static result_t clearImmediate(v8::Local<v8::Value> t);
static result_t call(v8::Local<v8::Function> func, double timeout, OptArgs args, v8::Local<v8::Value>& retVal);
public:
static void s__new(const v8::FunctionCallbackInfo<v8::Value>& args)
@@ -52,6 +53,7 @@ class timers_base : public object_base {
static void s_clearHrInterval(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_setImmediate(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_clearImmediate(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_call(const v8::FunctionCallbackInfo<v8::Value>& args);
};
}
@@ -68,7 +70,8 @@ inline ClassInfo& timers_base::class_info()
{ "setHrInterval", s_setHrInterval, true },
{ "clearHrInterval", s_clearHrInterval, true },
{ "setImmediate", s_setImmediate, true },
{ "clearImmediate", s_clearImmediate, true }
{ "clearImmediate", s_clearImmediate, true },
{ "call", s_call, true }
};
static ClassData s_cd = {
@@ -207,6 +210,24 @@ inline void timers_base::s_clearImmediate(const v8::FunctionCallbackInfo<v8::Val
METHOD_VOID();
}
inline void timers_base::s_call(const v8::FunctionCallbackInfo<v8::Value>& args)
{
v8::Local<v8::Value> vr;
METHOD_NAME("timers.call");
METHOD_ENTER();
METHOD_OVER(-1, 2);
ARG(v8::Local<v8::Function>, 0);
ARG(double, 1);
ARG_LIST(2);
hr = call(v0, v1, v2, vr);
METHOD_RETURN();
}
}
#endif
@@ -135,6 +135,9 @@ save_method_name::save_method_name(const char* name)
save_method_name::~save_method_name()
{
m_fb->m_native_name = m_name;
if (m_fb->m_termed && !m_fb->holder()->m_isolate->IsExecutionTerminating())
m_fb->holder()->m_isolate->TerminateExecution();
}
result_t JSFiber::get_id(int64_t& retVal)
@@ -0,0 +1,70 @@
/*
* Timer.cpp
*
* Created on: Dec 15, 2015
* Author: lion
*/
#include "object.h"
#include "utils.h"
#include "QuickArray.h"
#include "ifs/global.h"
#include "ifs/timers.h"
#include "Timer.h"
namespace fibjs {
class FuncTimer : public JSTimer {
public:
FuncTimer(JSFiber* fiber, int32_t timeout)
: JSTimer(timeout, false, true)
, m_fiber(fiber)
{
}
public:
virtual void on_js_timer()
{
m_fiber->m_termed = true;
if (JSFiber::current() == m_fiber)
m_fiber->holder()->m_isolate->TerminateExecution();
}
private:
obj_ptr<JSFiber> m_fiber;
};
result_t timers_base::call(v8::Local<v8::Function> func, double timeout,
OptArgs args, v8::Local<v8::Value>& retVal)
{
if (timeout < 1 || timeout > TIMEOUT_MAX)
timeout = 1;
Isolate* isolate = Isolate::current();
obj_ptr<JSFiber> this_fiber = JSFiber::current();
std::vector<v8::Local<v8::Value>> argv;
args.GetData(argv);
obj_ptr<JSTimer> timer = new FuncTimer(this_fiber, (int32_t)timeout);
TryCatch try_catch;
timer->sleep();
retVal = func->Call(v8::Undefined(isolate->m_isolate), (int32_t)argv.size(), argv.data());
timer->clear();
if (try_catch.HasCaught()) {
if (this_fiber->m_termed) {
this_fiber->m_termed = false;
isolate->m_isolate->CancelTerminateExecution();
return CHECK_ERROR(CALL_E_TIMEOUT);
} else {
try_catch.ReThrow();
return CHECK_ERROR(CALL_E_JAVASCRIPT);
}
}
return 0;
}
}
View
@@ -65,4 +65,12 @@ module timers
@param t 指定要清除的定时器
*/
static clearImmediate(Value t);
/*! @brief 调用给定的函数,并在超时时间到期时中断函数运行
@param func 指定要运行的函数
@param timeout 指定超时时间
@param args 额外的参数,传入到指定的 callback 内,可选。
@return 返回 func 的运行结果
*/
static Value call(Function func, Number timeout, ...args);
};

0 comments on commit 973270c

Please sign in to comment.