@@ -222,6 +222,15 @@ ACCESSOR(JSRegExp, GetSource, js_regexp()->kSourceOffset, String)
222222
223223ACCESSOR (JSDate, GetValue, js_date()->kValueOffset , Value)
224224
225+ bool String::IsString (LLV8* v8, HeapObject heap_object, Error& err) {
226+ if (!heap_object.Check ()) return false ;
227+
228+ int64_t type = heap_object.GetType (err);
229+ if (err.Fail ()) return false ;
230+
231+ return type < v8->types ()->kFirstNonstringType ;
232+ }
233+
225234inline int64_t String::Representation (Error& err) {
226235 int64_t type = GetType (err);
227236 if (err.Fail ()) return -1 ;
@@ -242,13 +251,47 @@ ACCESSOR(Script, LineOffset, script()->kLineOffsetOffset, Smi)
242251ACCESSOR (Script, Source, script()->kSourceOffset , HeapObject)
243252ACCESSOR (Script, LineEnds, script()->kLineEndsOffset , HeapObject)
244253
245- ACCESSOR (SharedFunctionInfo, Name , shared_info()->kNameOffset , String)
254+ ACCESSOR (SharedFunctionInfo, name , shared_info()->kNameOffset , String)
246255ACCESSOR (SharedFunctionInfo, InferredName, shared_info()->kInferredNameOffset ,
247256 Value)
248257ACCESSOR (SharedFunctionInfo, GetScript, shared_info()->kScriptOffset , Script)
249- ACCESSOR (SharedFunctionInfo, GetCode, shared_info()->kCodeOffset , Code)
250- ACCESSOR (SharedFunctionInfo, GetScopeInfo, shared_info()->kScopeInfoOffset ,
258+ ACCESSOR (SharedFunctionInfo, scope_info, shared_info()->kScopeInfoOffset ,
251259 HeapObject)
260+ ACCESSOR (SharedFunctionInfo, name_or_scope_info,
261+ shared_info ()->kNameOrScopeInfoOffset, HeapObject)
262+
263+
264+ HeapObject SharedFunctionInfo::GetScopeInfo(Error& err) {
265+ if (v8 ()->shared_info ()->kNameOrScopeInfoOffset == -1 ) return scope_info (err);
266+
267+ HeapObject maybe_scope_info = name_or_scope_info (err);
268+ if (!String::IsString (v8 (), maybe_scope_info, err)) return maybe_scope_info;
269+
270+ err = Error::Failure (" Couldn't get ScopeInfo" );
271+ return HeapObject ();
272+ }
273+
274+ String SharedFunctionInfo::Name (Error& err) {
275+ if (v8 ()->shared_info ()->kNameOrScopeInfoOffset == -1 ) return name (err);
276+
277+ HeapObject maybe_scope_info = name_or_scope_info (err);
278+ if (err.Fail ()) return String ();
279+
280+ if (String::IsString (v8 (), maybe_scope_info, err))
281+ return String (maybe_scope_info);
282+
283+ if (err.Fail ()) return String ();
284+
285+ HeapObject maybe_function_name =
286+ ScopeInfo (maybe_scope_info).MaybeFunctionName (err);
287+ if (err.Fail ()) return String ();
288+
289+ if (String::IsString (v8 (), maybe_function_name, err))
290+ return maybe_function_name;
291+
292+ err = Error::Failure (" Couldn't get SharedFunctionInfo's name" );
293+ return String ();
294+ }
252295
253296inline int64_t Code::Start () { return LeaField (v8 ()->code ()->kStartOffset ); }
254297
@@ -518,6 +561,32 @@ inline String ScopeInfo::ContextLocalName(int index, int param_count,
518561 return FixedArray::Get<String>(proper_index, err);
519562}
520563
564+ inline HeapObject ScopeInfo::MaybeFunctionName (Error& err) {
565+ int proper_index = v8 ()->scope_info ()->kVariablePartIndex +
566+ ParameterCount (err).GetValue () + 1 +
567+ StackLocalCount (err).GetValue () +
568+ (ContextLocalCount (err).GetValue () * 2 );
569+ // NOTE(mmarchini): FunctionName can be stored either in the first, second or
570+ // third slot after ContextLocalCount. Since there are missing postmortem
571+ // metadata to determine in which slot its being stored for the present
572+ // ScopeInfo, we try to find it heuristically.
573+ int tries = 3 ;
574+ while (tries > 0 ) {
575+ err = Error ();
576+
577+ HeapObject maybe_function_name =
578+ FixedArray::Get<HeapObject>(proper_index, err);
579+ if (err.Success () && String::IsString (v8 (), maybe_function_name, err))
580+ return maybe_function_name;
581+
582+ tries--;
583+ proper_index++;
584+ }
585+
586+ err = Error::Failure (" Couldn't get FunctionName from ScopeInfo" );
587+ return HeapObject ();
588+ }
589+
521590inline bool Oddball::IsHoleOrUndefined (Error& err) {
522591 Smi kind = Kind (err);
523592 if (err.Fail ()) return false ;
0 commit comments