001/* 002 * Stallion Core: A Modern Web Framework 003 * 004 * Copyright (C) 2015 - 2016 Stallion Software LLC. 005 * 006 * This program is free software: you can redistribute it and/or modify it under the terms of the 007 * GNU General Public License as published by the Free Software Foundation, either version 2 of 008 * the License, or (at your option) any later version. This program is distributed in the hope that 009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 011 * License for more details. You should have received a copy of the GNU General Public License 012 * along with this program. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>. 013 * 014 * 015 * 016 */ 017 018package io.stallion.plugins.javascript; 019 020import io.stallion.services.Log; 021import jdk.nashorn.internal.lookup.Lookup; 022import jdk.nashorn.internal.objects.Global; 023import jdk.nashorn.internal.runtime.Context; 024 025import java.lang.invoke.MethodHandle; 026import java.lang.invoke.MethodHandles; 027import java.lang.invoke.MethodType; 028 029import static io.stallion.utils.Literals.*; 030import static io.stallion.Context.*; 031 032 033public class EvalLoop { 034 private static Global global; 035 private static Context context; 036 037 038 private static MethodHandle CALC = null; 039 public static MethodHandle getCalcHandle() { 040 if (CALC == null) { 041 try { 042 CALC = MethodHandles.publicLookup().findStatic( 043 EvalLoop.class, "calc", MethodType.methodType(Object.class, new Class[]{Object.class, String.class, Object.class})); 044 //CALC = MethodHandles.publicLookup().findStatic(EvalLoop.class, "calc", 045 // 046 } catch(Exception e) { 047 throw new RuntimeException(e); 048 } 049 } 050 return CALC; 051 } 052 053 public static MethodHandle getCalcHandle2(Global global, Context context) { 054 EvalLoop.global = global; 055 EvalLoop.context = context; 056 return Lookup.MH.findStatic( 057 MethodHandles.lookup(), 058 EvalLoop.class, "calc", 059 Lookup.MH.type(Object.class, new Class[]{Object.class, String.class, Object.class} 060 )); 061 } 062 static int test() throws Throwable { 063 return 140; 064 } 065 public static Object calc(Object x, String s, Object c) { 066 Log.info("Object 1 {0} O2: {1} c: {2}", x, s, c); 067 return global.eval(x, s); 068 } 069}