< prev index next >

src/jdk/nashorn/internal/runtime/linker/Bootstrap.java

Print this page




 198      * invokedynamic instructions.
 199      * @param lookup MethodHandle lookup. Ignored as Nashorn only uses public lookup.
 200      * @param opDesc Dynalink dynamic operation descriptor.
 201      * @param type   Method type.
 202      * @param flags  flags for call type, trace/profile etc.
 203      * @return CallSite with MethodHandle to appropriate method or null if not found.
 204      */
 205     public static CallSite bootstrap(final Lookup lookup, final String opDesc, final MethodType type, final int flags) {
 206         return dynamicLinker.link(LinkerCallSite.newLinkerCallSite(lookup, opDesc, type, flags));
 207     }
 208 
 209     /**
 210      * Boostrapper for math calls that may overflow
 211      * @param lookup         lookup
 212      * @param name           name of operation
 213      * @param type           method type
 214      * @param programPoint   program point to bind to callsite
 215      *
 216      * @return callsite for a math intrinsic node
 217      */
 218     public static CallSite mathBootstrap(final MethodHandles.Lookup lookup, final String name, final MethodType type, final int programPoint) {
 219         final MethodHandle mh;
 220         switch (name) {
 221         case "iadd":
 222             mh = JSType.ADD_EXACT.methodHandle();
 223             break;
 224         case "isub":
 225             mh = JSType.SUB_EXACT.methodHandle();
 226             break;
 227         case "imul":
 228             mh = JSType.MUL_EXACT.methodHandle();
 229             break;
 230         case "idiv":
 231             mh = JSType.DIV_EXACT.methodHandle();
 232             break;
 233         case "irem":
 234             mh = JSType.REM_EXACT.methodHandle();
 235             break;
 236         case "ineg":
 237             mh = JSType.NEGATE_EXACT.methodHandle();
 238             break;
 239         case "ladd":
 240             mh = JSType.ADD_EXACT_LONG.methodHandle();
 241             break;
 242         case "lsub":
 243             mh = JSType.SUB_EXACT_LONG.methodHandle();
 244             break;
 245         case "lmul":
 246             mh = JSType.MUL_EXACT_LONG.methodHandle();
 247             break;
 248         case "ldiv":
 249             mh = JSType.DIV_EXACT_LONG.methodHandle();
 250             break;
 251         case "lrem":
 252             mh = JSType.REM_EXACT_LONG.methodHandle();
 253             break;
 254         case "lneg":
 255             mh = JSType.NEGATE_EXACT_LONG.methodHandle();
 256             break;
 257         default:
 258             throw new AssertionError("unsupported math intrinsic");
 259         }
 260         return new ConstantCallSite(MH.insertArguments(mh, mh.type().parameterCount() - 1, programPoint));
 261     }
 262 
 263     /**
 264      * Returns a dynamic invoker for a specified dynamic operation using the public lookup. You can use this method to
 265      * create a method handle that when invoked acts completely as if it were a Nashorn-linked call site. An overview of
 266      * available dynamic operations can be found in the
 267      * <a href="https://github.com/szegedi/dynalink/wiki/User-Guide-0.6">Dynalink User Guide</a>, but we'll show few
 268      * examples here:
 269      * <ul>
 270      *   <li>Get a named property with fixed name:
 271      *     <pre>
 272      * MethodHandle getColor = Boostrap.createDynamicInvoker("dyn:getProp:color", Object.class, Object.class);
 273      * Object obj = ...; // somehow obtain the object
 274      * Object color = getColor.invokeExact(obj);
 275      *     </pre>




 198      * invokedynamic instructions.
 199      * @param lookup MethodHandle lookup. Ignored as Nashorn only uses public lookup.
 200      * @param opDesc Dynalink dynamic operation descriptor.
 201      * @param type   Method type.
 202      * @param flags  flags for call type, trace/profile etc.
 203      * @return CallSite with MethodHandle to appropriate method or null if not found.
 204      */
 205     public static CallSite bootstrap(final Lookup lookup, final String opDesc, final MethodType type, final int flags) {
 206         return dynamicLinker.link(LinkerCallSite.newLinkerCallSite(lookup, opDesc, type, flags));
 207     }
 208 
 209     /**
 210      * Boostrapper for math calls that may overflow
 211      * @param lookup         lookup
 212      * @param name           name of operation
 213      * @param type           method type
 214      * @param programPoint   program point to bind to callsite
 215      *
 216      * @return callsite for a math intrinsic node
 217      */
 218     public static CallSite mathBootstrap(final Lookup lookup, final String name, final MethodType type, final int programPoint) {
 219         final MethodHandle mh;
 220         switch (name) {
 221         case "iadd":
 222             mh = JSType.ADD_EXACT.methodHandle();
 223             break;
 224         case "isub":
 225             mh = JSType.SUB_EXACT.methodHandle();
 226             break;
 227         case "imul":
 228             mh = JSType.MUL_EXACT.methodHandle();
 229             break;
 230         case "idiv":
 231             mh = JSType.DIV_EXACT.methodHandle();
 232             break;
 233         case "irem":
 234             mh = JSType.REM_EXACT.methodHandle();
 235             break;
 236         case "ineg":
 237             mh = JSType.NEGATE_EXACT.methodHandle();


















 238             break;
 239         default:
 240             throw new AssertionError("unsupported math intrinsic");
 241         }
 242         return new ConstantCallSite(MH.insertArguments(mh, mh.type().parameterCount() - 1, programPoint));
 243     }
 244 
 245     /**
 246      * Returns a dynamic invoker for a specified dynamic operation using the public lookup. You can use this method to
 247      * create a method handle that when invoked acts completely as if it were a Nashorn-linked call site. An overview of
 248      * available dynamic operations can be found in the
 249      * <a href="https://github.com/szegedi/dynalink/wiki/User-Guide-0.6">Dynalink User Guide</a>, but we'll show few
 250      * examples here:
 251      * <ul>
 252      *   <li>Get a named property with fixed name:
 253      *     <pre>
 254      * MethodHandle getColor = Boostrap.createDynamicInvoker("dyn:getProp:color", Object.class, Object.class);
 255      * Object obj = ...; // somehow obtain the object
 256      * Object color = getColor.invokeExact(obj);
 257      *     </pre>


< prev index next >