Commit 8b7bcc4e authored by yurys@chromium.org's avatar yurys@chromium.org

Add getters for column number and script id to v8::Function

Review URL: http://codereview.chromium.org/8508008

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@9935 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent aff88ef3
...@@ -1736,9 +1736,16 @@ class Function : public Object { ...@@ -1736,9 +1736,16 @@ class Function : public Object {
* kLineOffsetNotFound if no information available. * kLineOffsetNotFound if no information available.
*/ */
V8EXPORT int GetScriptLineNumber() const; V8EXPORT int GetScriptLineNumber() const;
/**
* Returns zero based column number of function body and
* kLineOffsetNotFound if no information available.
*/
V8EXPORT int GetScriptColumnNumber() const;
V8EXPORT Handle<Value> GetScriptId() const;
V8EXPORT ScriptOrigin GetScriptOrigin() const; V8EXPORT ScriptOrigin GetScriptOrigin() const;
static inline Function* Cast(Value* obj); static inline Function* Cast(Value* obj);
V8EXPORT static const int kLineOffsetNotFound; V8EXPORT static const int kLineOffsetNotFound;
private: private:
V8EXPORT Function(); V8EXPORT Function();
V8EXPORT static void CheckCast(Value* obj); V8EXPORT static void CheckCast(Value* obj);
......
...@@ -3619,6 +3619,23 @@ int Function::GetScriptLineNumber() const { ...@@ -3619,6 +3619,23 @@ int Function::GetScriptLineNumber() const {
} }
int Function::GetScriptColumnNumber() const {
i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
if (func->shared()->script()->IsScript()) {
i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
return i::GetScriptColumnNumber(script, func->shared()->start_position());
}
return kLineOffsetNotFound;
}
Handle<Value> Function::GetScriptId() const {
i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
if (!func->shared()->script()->IsScript())
return v8::Undefined();
i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
return Utils::ToLocal(i::Handle<i::Object>(script->id()));
}
int String::Length() const { int String::Length() const {
i::Handle<i::String> str = Utils::OpenHandle(this); i::Handle<i::String> str = Utils::OpenHandle(this);
if (IsDeadCheck(str->GetIsolate(), "v8::String::Length()")) return 0; if (IsDeadCheck(str->GetIsolate(), "v8::String::Length()")) return 0;
......
...@@ -662,6 +662,19 @@ int GetScriptLineNumber(Handle<Script> script, int code_pos) { ...@@ -662,6 +662,19 @@ int GetScriptLineNumber(Handle<Script> script, int code_pos) {
return right + script->line_offset()->value(); return right + script->line_offset()->value();
} }
// Convert code position into column number.
int GetScriptColumnNumber(Handle<Script> script, int code_pos) {
int line_number = GetScriptLineNumber(script, code_pos);
if (line_number == -1) return -1;
AssertNoAllocation no_allocation;
FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
line_number = line_number - script->line_offset()->value();
if (line_number == 0) return code_pos + script->column_offset()->value();
int prev_line_end_pos =
Smi::cast(line_ends_array->get(line_number - 1))->value();
return code_pos - (prev_line_end_pos + 1);
}
int GetScriptLineNumberSafe(Handle<Script> script, int code_pos) { int GetScriptLineNumberSafe(Handle<Script> script, int code_pos) {
AssertNoAllocation no_allocation; AssertNoAllocation no_allocation;
......
...@@ -292,6 +292,7 @@ Handle<FixedArray> CalculateLineEnds(Handle<String> string, ...@@ -292,6 +292,7 @@ Handle<FixedArray> CalculateLineEnds(Handle<String> string,
int GetScriptLineNumber(Handle<Script> script, int code_position); int GetScriptLineNumber(Handle<Script> script, int code_position);
// The safe version does not make heap allocations but may work much slower. // The safe version does not make heap allocations but may work much slower.
int GetScriptLineNumberSafe(Handle<Script> script, int code_position); int GetScriptLineNumberSafe(Handle<Script> script, int code_position);
int GetScriptColumnNumber(Handle<Script> script, int code_position);
// Computes the enumerable keys from interceptors. Used for debug mirrors and // Computes the enumerable keys from interceptors. Used for debug mirrors and
// by GetKeysInFixedArrayFor below. // by GetKeysInFixedArrayFor below.
......
...@@ -13748,6 +13748,41 @@ THREADED_TEST(ScriptLineNumber) { ...@@ -13748,6 +13748,41 @@ THREADED_TEST(ScriptLineNumber) {
} }
THREADED_TEST(ScriptColumnNumber) {
v8::HandleScope scope;
LocalContext env;
v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test"),
v8::Integer::New(3), v8::Integer::New(2));
v8::Handle<v8::String> script = v8::String::New(
"function foo() {}\n\n function bar() {}");
v8::Script::Compile(script, &origin)->Run();
v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::New("foo")));
v8::Local<v8::Function> bar = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::New("bar")));
CHECK_EQ(14, foo->GetScriptColumnNumber());
CHECK_EQ(17, bar->GetScriptColumnNumber());
}
THREADED_TEST(FunctionGetScriptId) {
v8::HandleScope scope;
LocalContext env;
v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test"),
v8::Integer::New(3), v8::Integer::New(2));
v8::Handle<v8::String> scriptSource = v8::String::New(
"function foo() {}\n\n function bar() {}");
v8::Local<v8::Script> script(v8::Script::Compile(scriptSource, &origin));
script->Run();
v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::New("foo")));
v8::Local<v8::Function> bar = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::New("bar")));
CHECK_EQ(script->Id(), foo->GetScriptId());
CHECK_EQ(script->Id(), bar->GetScriptId());
}
static v8::Handle<Value> GetterWhichReturns42(Local<String> name, static v8::Handle<Value> GetterWhichReturns42(Local<String> name,
const AccessorInfo& info) { const AccessorInfo& info) {
return v8_num(42); return v8_num(42);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment