BoxedExternal

Summary

Boxes an external value or reference as an internally-typed object.

Description

Consider the following:

class Foo
{
    private var _bar;
    public property function bar() {
        return _bar;
    }
}

The above code will break encapsulation. Even without a setter defined, the very fact that the reference to "bar" is escaped will violate encapsulation. Since externals can be any object or reference, it can be modified in unexpected ways. System.BoxedExternal is an internal type that boxes the external reference, so we can have references without exposing them as a naked external.

The whole point of BoxedExternal is that a BoxedExternal cannot implicitly "escape" to JavaScript without first unboxing - which is much easier to detect.

Examples

Basic Usage
1
2
3
4
5
6
7
8
import System;
 
var obj = {};
 
BoxedExternal boxed = new BoxedExternal(obj);
 
// var externalConversionAttempt1 = boxed;      // ERROR
var externalConversionAttempt2 = boxed.unbox(); // OK, explicit unboxing
Changing the boxed object at runtime
1
2
3
4
5
6
import System;
 
var obj1 = {}, obj2 = {};
BoxedExternal boxed = new BoxedExternal();
boxed.box(obj1);
boxed.box(obj2);
System.Object API
1
2
3
4
5
6
7
8
9
10
11
import System;
 
void internalTypesOnly(System.Object obj) {
    Console.log(obj.toString());
}
 
var myExternal = {};
BoxedExternal myInternal = new BoxedExternal(internalTypesOnly);
 
// internalTypesOnly(myExternal); // ERROR
internalTypesOnly(myInternal);    // OK

Methods

  • box

    Boxes an external object to give it an internal type.

  • BoxedExternal (Constructor)

    Constructs a System.BoxedExternal object.

  • unbox

    Unboxes the boxed external object to give it an external type.

Share

HTML | BBCode | Direct Link