Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2014 Red Hat, and individual contributors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.dynjs.runtime.java;
 
Provides Nashorn-like extensions in Javascript code. See: https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions

Example:

var obj = new JSAdapter({ var store = {}; __get__: function(name) { return store[name]; } __put__: function(name, value) { store[name] = value; } });
 
 public class JSAdapter extends BuiltinObject {
     public JSAdapter(GlobalContext globalContext) {
         super(globalContext);
     }
 
     @Override
     public Object call(ExecutionContext contextObject selfObject... args) {
         JSObject prototype = null;
         JSObject overrides = null;
         JSObject adaptee = null;
 
         if (args.length == 1) {
             adaptee = (JSObjectargs[0];
         } else if (args.length == 2) {
             overrides = (JSObjectargs[0];
             adaptee = (JSObjectargs[1];
         } else if (args.length == 3) {
             prototype = (JSObjectargs[0];
             overrides = (JSObjectargs[1];
             adaptee = (JSObjectargs[2];
         }
 
         return new JSAdapterObject(context.getGlobalContext(), prototypeoverridesadaptee);
     }
 
     public static DynObject newObject(ExecutionContext context) {
         return (DynObjectcontext.construct((Objectnull, (JSFunctioncontext.getGlobalContext().getObject().get(context"__JS_Adapter"));
     }
 
     @Override
     public void setFileName() {
         this. = "org/dynjs/runtime/java/JSAdapter.java";
     }
 
     @Override
     public void setupDebugContext() {
         this. = "<native function: JSAdapter>";
     }
 
     private class JSAdapterObject extends DynObject {
 
         private final JSObject overrides;
         private final JSObject adaptee;
 
         public JSAdapterObject(GlobalContext globalContextJSObject prototypeJSObject overridesObject adaptee) {
             super(globalContext);
             setClassName("JSObject");
             if (prototype != null) {
                 setPrototype(prototype);
             }
             if (overrides != null) {
                 this. = overrides;
             } else {
                 this. = null;
             }
 
             if ((adaptee instanceof JSObject)) {
                 this. = (JSObjectadaptee;
             } else {
                 this. = new DynObject(globalContext);
             }
         }
 
         @Override
         public Object get(ExecutionContext contextString name) {
             if ((this. != null) && (this..getProperty(contextname) != .)) {
                 return this..get(contextname);
             }
            if (getProperty(contextname) != .) {
                return super.get(contextname);
            }
            if (this. != null) {
                final Object getter = .get(context"__get__");
                if (getter != .) {
                    return context.call((JSFunctiongetterthisname);
                }
            }
            return super.get(contextname);
        }
        @Override
        public void put(ExecutionContext contextfinal String namefinal Object valuefinal boolean shouldThrow) {
            if ((this. != null) && (this..getProperty(contextname) != .)) {
                this..put(contextnamevalueshouldThrow);
                return;
            }
            if (getProperty(contextname) != .) {
                super.put(contextnamevalueshouldThrow);
                return;
            }
            if (this. != null) {
                final Object setter = .get(context"__set__");
                if (setter != .) {
                    context.call((JSFunctionsetterthisnamevalue);
                    return;
                }
            }
            super.put(contextnamevalueshouldThrow);
        }
    }
New to GrepCode? Check out our FAQ X