Example usage for jdk.nashorn.api.scripting ScriptObjectMirror unwrap

List of usage examples for jdk.nashorn.api.scripting ScriptObjectMirror unwrap

Introduction

In this page you can find the example usage for jdk.nashorn.api.scripting ScriptObjectMirror unwrap.

Prototype

public static Object unwrap(final Object obj, final Object homeGlobal) 

Source Link

Document

Unwrap a script object mirror if needed.

Usage

From source file:com.threecrickets.jvm.json.nashorn.ScriptObjectMirrorEncoder.java

License:Mozilla Public License

public void encode(Object object, JsonContext context) throws IOException {
    ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) object;

    Object wrapped = ScriptObjectMirror.unwrap(scriptObjectMirror, Context.getGlobal());
    if (!(wrapped instanceof ScriptObjectMirror)) {
        context.encode(wrapped);/*from   ww  w  . ja va 2  s  . c  o  m*/
        return;
    }

    if (scriptObjectMirror.isArray()) {
        context.out.append('[');

        int length = scriptObjectMirror.size();
        if (length > 0) {
            context.newline();

            for (int i = 0; i < length; i++) {
                Object value = scriptObjectMirror.getSlot(i);

                context.indentNested();
                context.nest().encode(value);

                if (i < length - 1)
                    context.comma();
            }

            context.newline();
            context.indent();
        }

        context.out.append(']');
    } else {
        context.out.append('{');

        String[] keys = scriptObjectMirror.getOwnKeys(true);
        int length = keys.length;
        if (length > 0) {
            context.newline();

            for (int i = 0; i < length; i++) {
                String key = keys[i];
                Object value = scriptObjectMirror.get(key);

                context.indentNested();
                context.quoted(key);
                context.colon();
                context.nest().encode(value);

                if (i < length - 1)
                    context.comma();
            }

            context.newline();
            context.indent();
        }

        context.out.append('}');
    }
}

From source file:com.threecrickets.jvm.json.nashorn.ScriptObjectMirrorTransformer.java

License:Mozilla Public License

public Object transform(Object object, JsonImplementation implementation) {
    if (object instanceof ScriptObjectMirror) {
        ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) object;

        Object wrapped = ScriptObjectMirror.unwrap(scriptObjectMirror, Context.getGlobal());
        if (!(wrapped instanceof ScriptObjectMirror)) {
            for (JsonTransformer transformer : implementation.getTransformers()) {
                Object r = transformer.transform(wrapped, implementation);
                if (r != null)
                    return r;
            }//from  ww w.  ja  va  2  s  .  c  o m
        }
    }

    return null;
}

From source file:com.threecrickets.jvm.json.nashorn.util.NashornNativeUtil.java

License:Mozilla Public License

public static Object unwrap(Object object) {
    // Nashorn creates these mirrors when they pass through certain
    // boundaries. However, we are only allowed to unwrap them from the same
    // global context in which they were wrapped. We will try to unwrap them
    // here, but if that doesn't work we will just create a shallow clone.
    // The clone won't function like the original, but will be good enough
    // for our purposes here.

    object = ScriptObjectMirror.unwrap(object, Context.getGlobal());
    if (object instanceof ScriptObjectMirror) {
        ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) object;
        ScriptObject scriptObject = NashornNativeUtil.newObject();
        for (String key : scriptObjectMirror.getOwnKeys(true)) {
            Object value = scriptObjectMirror.get(key);
            scriptObject.put(key, value, false);
        }//  w  w  w.j a v a 2s  . c  o m
        object = scriptObject;
    }
    return object;
}

From source file:org.bson.jvm.nashorn.ScriptObjectMirrorCodec.java

License:Apache License

@SuppressWarnings("unchecked")
public void encode(BsonWriter writer, ScriptObjectMirror scriptObjectMirror, EncoderContext encoderContext) {
    Object wrapped = ScriptObjectMirror.unwrap(scriptObjectMirror, Context.getGlobal());
    if (!(wrapped instanceof ScriptObjectMirror)) {
        // Attempt to encode the wrapped object
        Codec<Object> codec = null;
        try {//from   w ww  .j  av  a 2  s. c o  m
            codec = (Codec<Object>) codecRegistry.get(wrapped.getClass());
        } catch (CodecConfigurationException x) {
        }
        if (codec != null) {
            codec.encode(writer, wrapped, encoderContext);
            return;
        }
    }

    if (scriptObjectMirror.isArray()) {
        writer.writeStartArray();
        for (int i = 0, length = scriptObjectMirror.size(); i < length; i++) {
            Object item = scriptObjectMirror.getSlot(i);
            BsonUtil.writeChild(item, writer, encoderContext, codecRegistry);
        }
        writer.writeEndArray();
    } else {
        writer.writeStartDocument();
        for (String key : scriptObjectMirror.getOwnKeys(true)) {
            Object value = scriptObjectMirror.get(key);
            writer.writeName(key);
            BsonUtil.writeChild(value, writer, encoderContext, codecRegistry);
        }
        writer.writeEndDocument();
    }
}

From source file:ptolemy.actor.lib.jjs.HelperBase.java

License:Open Source License

/** Construct a helper for the specified JavaScript object.
 *  @param currentObj The JavaScript object that this is helping.
 *///www  . j a v a 2s . c om
public HelperBase(ScriptObjectMirror currentObj) {
    _currentObj = currentObj;

    Object actorOrWrapper = _currentObj.eval("actor");
    if (actorOrWrapper instanceof ScriptObjectMirror) {
        actorOrWrapper = ScriptObjectMirror.unwrap(actorOrWrapper, ScriptContext.ENGINE_SCOPE);
    }
    if (actorOrWrapper instanceof RestrictedJavaScriptInterface) {
        _actor = ((RestrictedJavaScriptInterface) actorOrWrapper)._getActor();
    } else if (actorOrWrapper instanceof JavaScript) {
        _actor = ((JavaScript) actorOrWrapper);
    } else {
        throw new InternalErrorException("Invalid actor object: " + actorOrWrapper.toString());
    }
}