summaryrefslogtreecommitdiffstats
path: root/webkit/port/bindings/v8/extensions/Interval.cpp
blob: 4a5c13d4106b446022e5a3178c85fa7cd628780f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "config.h"
#include "Interval.h"
#include "wtf/CurrentTime.h"

namespace WebCore {

const char* kIntervalExtensionName = "v8/Interval";

class IntervalExtensionWrapper : public v8::Extension {
public:
    IntervalExtensionWrapper() : 
        v8::Extension(kIntervalExtensionName,
          "var chromium;"
          "if (!chromium)"
          "  chromium = {};"
          "chromium.Interval = function() {"
          "  var start_ = 0;"
          "  var stop_ = 0;"
          "  native function HiResTime();"
          "  this.start = function() {"
          "    stop_ = 0;"
          "    start_ = HiResTime();"
          "  };"
          "  this.stop = function() {"
          "    stop_ = HiResTime();"
          "    if (start_ == 0)"
          "      stop_ = 0;"
          "  };"
          "  this.microseconds = function() {"
          "    var stop = stop_;"
          "    if (stop == 0 && start_ != 0)"
          "      stop = HiResTime();"
          "    return Math.ceil((stop - start_) * 1000000);"
          "  };"
          "}") {};

    virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(v8::Handle<v8::String> name) {
        if (name->Equals(v8::String::New("HiResTime")))
            return v8::FunctionTemplate::New(HiResTime);
        return v8::Handle<v8::FunctionTemplate>();
    }

    static v8::Handle<v8::Value> HiResTime(const v8::Arguments& args) {
        return v8::Number::New(WTF::currentTime());
    }
};

v8::Extension*  IntervalExtension::Get() {
    return new IntervalExtensionWrapper();
}

}