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

前端 未结 5 1740
既然无缘
既然无缘 2020-12-31 15:31

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 en

相关标签:
5条回答
  • 2020-12-31 16:00

    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.

    0 讨论(0)
  • 2020-12-31 16:03

    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');​
    
    0 讨论(0)
  • 2020-12-31 16:09

    I believe that the very nature of host objects means there is not a simple, environment-agnostic way to detect them. See this discussion on SO for more, if you're curious.

    As you have noted, the jQuery project has also tried to detect host objects and run into similar troubles. The discussion on that bug page is very telling.

    0 讨论(0)
  • 2020-12-31 16:12

    I have an idea that may not be applicable in all contexts.

    Make sure your script is the first one to execute, and wrap it in a closure, much like JS frameworks do.
    Then, loop through all objects in your global scope (If you are on something that's not a browser, window will be undefined; hence at the beggining of the script do a window = this), and loop through its children, and so on. All objects except your will be host objects! then you can add that to a local database or even store it and associate it with the running environment for future use.

    0 讨论(0)
  • 2020-12-31 16:22

    ALMOST SOLVED

    Almost managed to get this to work.

    The solution falls short in that Host objects are sometimes undistinguishable from Native ones. The code below fails when testing isNative(window.alert) on Chrome as webkit engine defines an alert function that (so far) looks identical to a native one.

    It uses plain javascript as per ES3 and is based on testing that an object is native (as opposed to Host object). However, as per ES3 definition of Host Objects: 'Any object that is not native is a host object.' this function can be used to detect host objects.

    // ISNATIVE OBJECT DETECTION
    
    function isNative(obj) {
    
        switch(typeof obj) {
            case 'number': case 'string': case 'boolean':
                // Primitive types are not native objects
                return false;
        }  
    
        // 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;
    
        return true;
    }
    
    // CHECK IF AN OBJECT IS HOST OR NATIVE
    
    if (typeof myObject === 'object' || typeof myObject === 'function')
       alert(isNative(myObject) ? 'Native Object' : 'Host Object'); 
    

    Here is a list of JsFiddle tests that can be used to test this in IE / Firefox / Chrome.

    I havent tested Non-browser environments as its a bit more of a hassle but since the code is so basic I dont think it'll have any problems.

    // 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(isNative({}), '{} -- Plain object');
    assert(isNative(function() {}), 'function() {} -- Plain function');
    assert(isNative([]), '[] -- Plain array');
    
    assert(isNative(/regex/), '/regex/ - Native regex');
    assert(isNative(new Date()), 'new Date() - Native date object through instantiation');
    
    assert(isNative(new String('string')), 'new String("string") - Native string object through instantiation');
    assert(isNative(new Number(1)), 'new Number(1) - Native number object through instantiation');
    assert(isNative(new Boolean(true)), 'new Boolean(true) - Native boolean object through instantiation');
    assert(isNative(new Array()), 'new Array() - Native array object through instantiation');
    assert(isNative(new Object()), '{} -- new Object() - Native object through instantiation');
    assert(isNative(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(isNative(Animal), 'Animal -- User defined type');
    assert(isNative(animal), 'animal -- Instance of User defined type');
    
    assert(isNative(Dog), 'Dog -- User defined inherited type');
    assert(isNative(dog), 'dog -- Instance of User defined inherited type');
    
    // BUILT IN OBJECTS
    
    assert(isNative(Object), 'Object -- Built in');
    assert(isNative(Array), 'Array -- Built in');
    assert(isNative(Date), 'Date -- Built in');
    assert(isNative(Boolean), 'Boolean -- Built in');
    assert(isNative(String), 'String -- Built in');
    assert(isNative(Function), 'Function -- Built in');
    
    // PRIMITIVE TYPES 
    
    assert(!isNative('string'), '"string" - Primitive string');
    assert(!isNative(1), '1 - Primitive number');
    assert(!isNative(true), 'true - Primitive boolean');
    assert(!isNative(null), 'null - Primitive null');
    assert(!isNative(NaN), 'NaN - Primitive number NotANumber');
    assert(!isNative(Infinity), 'Infinity - Primitive number Infinity');
    assert(!isNative(undefined), 'undefined - Primitive value undefined');
    
    // HOST OBJECTS
    
    assert(!isNative(window), 'window -- Host object');
    assert(!isNative(alert), 'alert -- Host function'); // fails on chrome
    assert(!isNative(document), 'document -- Host object');
    assert(!isNative(location), 'location -- Host object');
    assert(!isNative(navigator), 'navigator -- Host object');
    assert(!isNative(parent), 'parent -- Host object');
    assert(!isNative(frames), 'frames -- Host object');
    
    0 讨论(0)
提交回复
热议问题