Changeset 9107

Show
Ignore:
Timestamp:
06/10/08 13:38:27 (5 months ago)
Author:
hannes
Message:

* Remove JSAdapter magic from module scopes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • helma-ng/trunk/src/org/helma/javascript/ModuleScope.java

    r9037 r9107  
    1919import org.helma.repository.Repository; 
    2020import org.helma.repository.Resource; 
    21 import org.mozilla.javascript.*; 
     21import org.mozilla.javascript.Context; 
     22import org.mozilla.javascript.NativeObject; 
     23import org.mozilla.javascript.Scriptable; 
    2224 
    2325/** 
     
    2931    Resource resource; 
    3032    Repository repository; 
    31     boolean hasAdapterFunctions; 
    32     AdapterFlag isAdapter = new AdapterFlag(); 
     33    long checksum; 
    3334    private static final long serialVersionUID = -2409425841990094897L; 
    3435 
     
    4041        setPrototype(prototype); 
    4142        defineProperty("__name__", moduleName, DONTENUM); 
    42         String pathName = resource == null ? 
    43                 repository.getName() : resource.getName(); 
    44         defineProperty("__path__", pathName, DONTENUM); 
     43        defineProperty("__path__", repository.getName(), DONTENUM); 
    4544    } 
    4645 
    4746    public Repository getRepository() { 
    4847        return repository; 
     48    } 
     49 
     50    public long getChecksum() { 
     51        return checksum; 
     52    } 
     53 
     54    public void setChecksum(long checksum) { 
     55        this.checksum = checksum; 
    4956    } 
    5057 
     
    6168    } 
    6269 
    63     public void setHasAdapterFunctions(boolean adapter) { 
    64         this.hasAdapterFunctions = adapter; 
    65         isAdapter = new AdapterFlag(); 
    66     } 
    67  
    68     public boolean getHasAdapterFunctions() { 
    69         return hasAdapterFunctions; 
    70     } 
    71  
    7270    public Object get(String name, Scriptable start) { 
    73         if (hasAdapterFunctions && isAdapter.get()) { 
    74             Function func = getAdapteeFunction(GET_PROP); 
    75             if (func != null) { 
    76                 return call(func, new Object[] { name }); 
    77             } 
    78         } 
    7971        Object value = super.get(name, start); 
    8072        if (value == NOT_FOUND) { 
     
    8880        return value; 
    8981    } 
    90  
    91     public Object get(int index, Scriptable start) { 
    92         if (hasAdapterFunctions && isAdapter.get()) { 
    93             Function func = getAdapteeFunction(GET_PROP); 
    94             if (func != null) { 
    95                 return call(func, new Object[] { index }); 
    96             } 
    97         } 
    98         return super.get(index, start); 
    99     } 
    100  
    101     public boolean has(String name, Scriptable start) { 
    102         if (hasAdapterFunctions && isAdapter.get()) { 
    103             Function func = getAdapteeFunction(HAS_PROP); 
    104             if (func != null) { 
    105                 Object res = call(func, new Object[] { name }); 
    106                 return Context.toBoolean(res); 
    107             } 
    108         } 
    109         return super.has(name, start); 
    110     } 
    111  
    112     public boolean has(int index, Scriptable start) { 
    113         if (hasAdapterFunctions && isAdapter.get()) { 
    114             Function func = getAdapteeFunction(HAS_PROP); 
    115             if (func != null) { 
    116                 Object res = call(func, new Object[] { index }); 
    117                 return Context.toBoolean(res); 
    118             } 
    119         } 
    120         return super.has(index, start); 
    121     } 
    122  
    123     public void put(String name, Scriptable start, Object value) { 
    124         if (start == this) { 
    125             if (hasAdapterFunctions && isAdapter.get()) { 
    126                 Function func = getAdapteeFunction(PUT_PROP); 
    127                 if (func != null) { 
    128                     call(func, new Object[] { name, value }); 
    129                     return; 
    130                 } 
    131             } 
    132             super.put(name, start, value); 
    133         } else { 
    134             start.put(name, start, value); 
    135         } 
    136     } 
    137  
    138     public void put(int index, Scriptable start, Object value) { 
    139         if (start == this) { 
    140             if (hasAdapterFunctions && isAdapter.get()) { 
    141                 Function func = getAdapteeFunction(PUT_PROP); 
    142                 if( func != null) { 
    143                     call(func, new Object[] { index, value }); 
    144                     return; 
    145                 } 
    146             } 
    147             super.put(index, start, value); 
    148         } else { 
    149             start.put(index, start, value); 
    150         } 
    151     } 
    152  
    153     public void delete(String name) { 
    154         if (hasAdapterFunctions && isAdapter.get()) { 
    155             Function func = getAdapteeFunction(DEL_PROP); 
    156             if (func != null) { 
    157                 call(func, new Object[] { name }); 
    158                 return; 
    159             } 
    160         } 
    161         super.delete(name); 
    162     } 
    163  
    164     public void delete(int index) { 
    165         if (hasAdapterFunctions && isAdapter.get()) { 
    166             Function func = getAdapteeFunction(DEL_PROP); 
    167             if (func != null) { 
    168                 call(func, new Object[] { index }); 
    169                 return; 
    170             } 
    171         } 
    172         super.delete(index); 
    173     } 
    174  
    175     public Object[] getIds() { 
    176         if (hasAdapterFunctions && isAdapter.get()) { 
    177             Function func = getAdapteeFunction(GET_PROPIDS); 
    178             if (func != null) { 
    179                 Object val = call(func, RhinoEngine.EMPTY_ARGS); 
    180                 // in most cases, adaptee would return native JS array 
    181                 if (val instanceof NativeArray) { 
    182                     NativeArray array = (NativeArray) val; 
    183                     Object[] res = new Object[(int)array.getLength()]; 
    184                     for (int index = 0; index < res.length; index++) { 
    185                         res[index] = mapToId(array.get(index, array)); 
    186                     } 
    187                     return res; 
    188                 } else if (val instanceof NativeJavaArray) { 
    189                     // may be attempt wrapped Java array 
    190                     Object tmp = ((NativeJavaArray)val).unwrap(); 
    191                     Object[] res; 
    192                     if (tmp.getClass() == Object[].class) { 
    193                         Object[]  array = (Object[]) tmp; 
    194                         res = new Object[array.length]; 
    195                         for (int index = 0; index < array.length; index++) { 
    196                             res[index] = mapToId(array[index]); 
    197                         } 
    198                     } else { 
    199                         // just return an empty array 
    200                         res = Context.emptyArgs; 
    201                     } 
    202                     return res; 
    203                 } else { 
    204                     // some other return type, just return empty array 
    205                     return Context.emptyArgs; 
    206                 } 
    207             } 
    208         } 
    209         return super.getIds(); 
    210     } 
    211  
    212     private Function getAdapteeFunction(String name) { 
    213         Object o = super.get(name, this); 
    214         if (o instanceof Function) return (Function) o; 
    215         Scriptable proto = getPrototype(); 
    216         if (proto != null) { 
    217             o = ScriptableObject.getProperty(proto, name); 
    218         } 
    219         return (o instanceof Function)? (Function)o : null; 
    220     } 
    221  
    222     private Object call(Function func, Object[] args) { 
    223         Context cx = Context.getCurrentContext(); 
    224         try { 
    225             isAdapter.set(false); 
    226             return func.call(cx, this, this, args); 
    227         } catch (RhinoException re) { 
    228             throw Context.reportRuntimeError(re.getMessage()); 
    229         } finally { 
    230             isAdapter.set(true); 
    231         } 
    232     } 
    233  
    234     // map a property id. Property id can only be an Integer or String 
    235     private Object mapToId(Object tmp) { 
    236         if (tmp instanceof Number) { 
    237             return ((Number)tmp).intValue(); 
    238         } else { 
    239             return Context.toString(tmp); 
    240         } 
    241     } 
    242  
    243     class AdapterFlag extends ThreadLocal<Boolean> { 
    244         protected Boolean initialValue() { 
    245             return hasAdapterFunctions; 
    246         } 
    247     }     
    248  
    249     // names of adaptee JavaScript functions 
    250     private static final String GET_PROP = "__get__"; 
    251     private static final String HAS_PROP = "__has__"; 
    252     private static final String PUT_PROP = "__put__"; 
    253     private static final String DEL_PROP = "__delete__"; 
    254     private static final String GET_PROPIDS = "__getIds__"; 
    255  
    25682} 
  • helma-ng/trunk/src/org/helma/javascript/ReloadableScript.java

    r9099 r9107  
    4242    // the loaded module scope is cached for shared modules 
    4343    ModuleScope moduleScope = null; 
    44     // script type - one of UNKNOW, ORDINARY, JSADAPTER 
    45     short scriptType = UNKNOWN; 
    46  
    47     final static short UNKNOWN = 0, 
    48                      ORDINARY = 1, 
    49                      JSADAPTER = 2; 
     44 
    5045 
    5146    /** 
     
    9792                exception = x; 
    9893            } finally { 
    99                 scriptType = UNKNOWN; 
    10094                checksum = resource.lastModified(); 
    10195            } 
     
    136130            exception = x; 
    137131        } finally { 
    138             scriptType = UNKNOWN; 
    139132            checksum = repository.getChecksum(); 
    140133        } 
     
    188181        if (module != null) { 
    189182            // use cached scope unless script has been reloaded 
    190             if (scriptType != UNKNOWN) { 
     183            if (module.getChecksum() == checksum) { 
    191184                return module; 
    192185            } 
     
    197190        modules.put(moduleName, module); 
    198191        script.exec(cx, module); 
    199         // find out if this is a JSAdapter type scope 
    200         if (scriptType == UNKNOWN) { 
    201             scriptType = module.has("__get__", module) || 
    202                          module.has("__has__", module) || 
    203                          module.has("__put__", module) || 
    204                          module.has("__delete__", module) || 
    205                          module.has("__getIds__", module) ? JSADAPTER : ORDINARY; 
    206         } 
    207         module.setHasAdapterFunctions(scriptType == JSADAPTER); 
     192        module.setChecksum(checksum); 
    208193        moduleScope = (module.get("__shared__", module) == Boolean.TRUE) ? 
    209194                module : null;