758 // Create the utility context for holding JavaScript functions used internally
759 // which are not visible to JavaScript executing on the page.
760 void V8Proxy::createUtilityContext()
761 {
762 ASSERT(m_utilityContext.IsEmpty());
763
764 v8::HandleScope scope;
765 v8::Handle<v8::ObjectTemplate> globalTemplate = v8::ObjectTemplate::New();
766 m_utilityContext = v8::Context::New(0, globalTemplate);
767 v8::Context::Scope contextScope(m_utilityContext);
768
769 // Compile JavaScript function for retrieving the source line of the top
770 // JavaScript stack frame.
771 DEFINE_STATIC_LOCAL(const char*, frameSourceLineSource,
772 ("function frameSourceLine(exec_state) {"
773 " if (!exec_state.frameCount())"
774 " return undefined;"
775 " return exec_state.frame(0).sourceLine();"
776 "}"));
777 v8::Script::Compile(v8::String::New(frameSourceLineSource))->Run();
778
779 // Compile JavaScript function for retrieving the source name of the top
780 // JavaScript stack frame.
781 DEFINE_STATIC_LOCAL(const char*, frameSourceNameSource,
782 ("function frameSourceName(exec_state) {"
783 " if (!exec_state.frameCount())"
784 " return undefined;"
785 " var frame = exec_state.frame(0);"
786 " if (frame.func().resolved() && "
787 " frame.func().script() && "
788 " frame.func().script().name()) {"
789 " return frame.func().script().name();"
790 " }"
791 "}"));
792 v8::Script::Compile(v8::String::New(frameSourceNameSource))->Run();
793 }
794
795 bool V8Proxy::sourceLineNumber(int& result)
796 {
797 v8::HandleScope scope;
798 v8::Handle<v8::Context> v8UtilityContext = V8Proxy::utilityContext();
799 if (v8UtilityContext.IsEmpty())
800 return false;
801 v8::Context::Scope contextScope(v8UtilityContext);
802 v8::Handle<v8::Function> frameSourceLine;
803 frameSourceLine = v8::Local<v8::Function>::Cast(v8UtilityContext->Global()->Get(v8::String::New("frameSourceLine")));
804 if (frameSourceLine.IsEmpty())
805 return false;
806 v8::Handle<v8::Value> value = v8::Debug::Call(frameSourceLine);
807 if (value.IsEmpty())
808 return false;
809 result = value->Int32Value();
810 return true;
811 }
812
813 bool V8Proxy::sourceName(String& result)
814 {
815 v8::HandleScope scope;
816 v8::Handle<v8::Context> v8UtilityContext = utilityContext();
817 if (v8UtilityContext.IsEmpty())
818 return false;
819 v8::Context::Scope contextScope(v8UtilityContext);
820 v8::Handle<v8::Function> frameSourceName;
821 frameSourceName = v8::Local<v8::Function>::Cast(v8UtilityContext->Global()->Get(v8::String::New("frameSourceName")));
822 if (frameSourceName.IsEmpty())
823 return false;
824 v8::Handle<v8::Value> value = v8::Debug::Call(frameSourceName);
825 if (value.IsEmpty())
826 return false;
827 result = toWebCoreString(value);
828 return true;
829 }
830