Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an environment-agnostic way to detect Javascript Host Objects?

I'm writing a Javascript stacktrace library. The library needs to detect wether a particular object or function was created by the programmer or was there as part of the environment (including built-in objects). Host objects are becoming a bit problematic due to their unpredictable behaviour, so I'm after an environment-agnostic way to determine if a particular object in Javascript is a host object (see ECMAScript 3 - 4.3.8). However, distinguishing host objects from native objects and primitive values is useful for programmers in other projects, particularly in browser-less environments, so I'd like to focus on that, rather than the problems host objects cause in my library or on distinguishing programmer-created objects.

So far I've only been able to come up with solutions that depend on the environment running javascript code. For example:

// IE Only: does not implement valueOf() in Host Objects
var isHost = (typeof obj === 'object' && typeof obj.valueOf === 'undefined');

// Firefox Only: Host objects have own constructor
var isHost = (obj.constructor && obj.hasOwnProperty('constructor'));

I noticed that jQuery's own isPlainObject() method is also dependent on environment, and that the logic is rather convoluted.

Perhaps this is because thats the nature of the beast with host objects (since their behaviour is defined by the environment), but I would like to dig a bit further to see if this is possible and was wondering if somebody has run across this particular problem before and has a solution ready.

So. Does anybody know a simple platform-independent solution to test for Host Objects? And if it runs in a browser-less environment such as Node or Rhino all the better for it.

Possible approaches (that may not work):

  • Testing for characteristics of host objects seems like a lost cause, given that there is no specification for their behaviour, however testing whether the object is part of the ES3 specification may be a possibility.
  • I have tried using Object.prototype.toString() given that its defined quite specifically, but the results are inconclusive as some environments (namely IE) choose to return the same value for native and host objects.
  • It may be possible to do this by checking whether the ultimate constructor of an object through the prototype chain really is an instanceof Function.
like image 695
Steven de Salas Avatar asked Dec 27 '11 14:12

Steven de Salas


2 Answers

When you look at the definition of host object — "object supplied by the host environment to complete the execution environment of ECMAScript." — it becomes pretty clear that there's no simple way to determine if an object is host or native.

Unlike native objects, host objects define internal properties (such as [[Prototype]], [[Class]], etc.) in implementation-specific way. That's because specification allows them to do this. However, there's no "MUST" requirement for host objects to implement internal behavior in implementation-specific way; it's a "MAY" type of requirement. So we can't rely on this. These objects may or may not act "weird". There's no way to tell.

There have been few attempts to detect host objects in the past, but all of them obviously rely on observations of certain environments (MSHTML DOM being one of them) — remember that host objects don't have any kind of unique pattern/trait to identify by. Peter Michaux documented most of the inferences here (take a look at "Feature Testing a Host Object" section). The infamous typeof ... == "unknown" comes from MSHTML DOM and its ActiveX -based host objects. Note that Peter talks about host objects mainly in context of browser scripting, and he narrows the checks down to "is this a host method?", "is this a host collection object", etc.

In some environments host objects don't inherit from Object.prototype (making it easy to check against), or have certain properties that throw errors (e.g. "prototype" on some "interface" objects in IE), or even throw errors themselves on access.

It might seem like you could just check if an object is one of those defined in specification, and if not, deem it as host. But that won't really help; it would only give you objects that aren't built-in. Some of these non-standard objects could still be native (meaning that they would implement usual semantics as described in specification).

Your best bet would be to test for particular behavior of your app/script, that host objects may be sensitive to. This is always the safest way. Are you planning to access something off of an object? Delete something from object? Add something to object? Test for it. See if it works. If it doesn't — you're likely dealing with host object.

like image 171
kangax Avatar answered Sep 20 '22 11:09

kangax


Here's a newer version of isNative that rejects all objects with a native implementation for toString, which solves the problem for the stack trace library but doesn't answer the question posted here satisfactorily. Where this approach fails for the latter is it filters out all built-in type definitions such as Object, Date, String, Math, etc., which are not host objects themselves. Also, this solution is dependent on how the environment outputs native/built-in function definitions (it must include "[native code]" for the function to work). Since the behaviour of the function is different, it's been renamed isUserObject.

// USER OBJECT DETECTION

function isUserObject(obj) {

    // Should be an instance of an Object
    if (!(obj instanceof Object)) return false;

    // Should have a constructor that is an instance of Function
    if (typeof obj.constructor === 'undefined') return false;
    if (!(obj.constructor instanceof Function)) return false;

    // Avoid built-in functions and type definitions
    if (obj instanceof Function && 
      Function.prototype.toString.call(obj).indexOf('[native code]') > -1) 
          return false;

    return true;
}

// CHECK IF AN OBJECT IS USER-CREATED OR NOT

if (typeof myObject === 'object' || typeof myObject === 'function')
   alert(isUserObject(myObject) ? 'User Object' : 'Non-user Object'); 

Here is a list of JsFiddle tests that can be used to test this in various browsers.

// ASSERT HELPER FUNCTION

var n = 0;
function assert(condition, message) {
    n++;
    if (condition !== true) {
       document.write(n + '. FAILS: ' + (message || '(no message)') + '<br/>');
    } else {
       document.write(n + '. PASS: ' + (message || '(no message)') + '<br/>');
    }
}

// USER CREATED OBJECTS

assert(isUserObject({}), '{} -- Plain object');
assert(isUserObject(function() {}), 'function() {} -- Plain function');
assert(isUserObject([]), '[] -- Plain array');

assert(isUserObject(/regex/), '/regex/ - Native regex');
assert(isUserObject(new Date()), 'new Date() - Native date object through instantiation');

assert(isUserObject(new String('string')), 'new String("string") - Native string object through instantiation');
assert(isUserObject(new Number(1)), 'new Number(1) - Native number object through instantiation');
assert(isUserObject(new Boolean(true)), 'new Boolean(true) - Native boolean object through instantiation');
assert(isUserObject(new Array()), 'new Array() - Native array object through instantiation');
assert(isUserObject(new Object()), '{} -- new Object() - Native object through instantiation');
assert(isUserObject(new Function('alert(1)')), '{} -- Native function through instantiation');

// USER OBJECT INSTANTIATION AND INHERITANCE

var Animal = function() {};
var animal = new Animal();

var Dog = function() {};
Dog.prototype = animal;
var dog = new Dog();

assert(isUserObject(Animal), 'Animal -- User defined type');
assert(isUserObject(animal), 'animal -- Instance of User defined type');

assert(isUserObject(Dog), 'Dog -- User defined inherited type');
assert(isUserObject(dog), 'dog -- Instance of User defined inherited type');

// BUILT IN OBJECTS

assert(!isUserObject(Object), 'Object -- Built in');
assert(!isUserObject(Array), 'Array -- Built in');
assert(!isUserObject(Date), 'Date -- Built in');
assert(!isUserObject(Boolean), 'Boolean -- Built in');
assert(!isUserObject(String), 'String -- Built in');
assert(!isUserObject(Function), 'Function -- Built in');

// PRIMITIVE TYPES 

assert(!isUserObject('string'), '"string" - Primitive string');
assert(!isUserObject(1), '1 - Primitive number');
assert(!isUserObject(true), 'true - Primitive boolean');
assert(!isUserObject(null), 'null - Primitive null');
assert(!isUserObject(NaN), 'NaN - Primitive number NotANumber');
assert(!isUserObject(Infinity), 'Infinity - Primitive number Infinity');
assert(!isUserObject(undefined), 'undefined - Primitive value undefined');

// HOST OBJECTS

assert(!isUserObject(window), 'window -- Host object');
assert(!isUserObject(alert), 'alert -- Host function');
assert(!isUserObject(document), 'document -- Host object');
assert(!isUserObject(location), 'location -- Host object');
assert(!isUserObject(navigator), 'navigator -- Host object');
assert(!isUserObject(parent), 'parent -- Host object');
assert(!isUserObject(frames), 'frames -- Host object');​
like image 29
2 revs Avatar answered Sep 19 '22 11:09

2 revs