1 /* $RCSfile$ 2 * $Author$ 3 * $Date$ 4 * $Revision$ 5 * 6 * Copyright (C) 2005 The Jmol Development Team 7 * 8 * Contact: jmol-developers@lists.sf.net 9 * 10 * This library is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU Lesser General Public 12 * License as published by the Free Software Foundation; either 13 * version 2.1 of the License, or (at your option) any later version. 14 * 15 * This library is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 * Lesser General Public License for more details. 19 * 20 * You should have received a copy of the GNU Lesser General Public 21 * License along with this library; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 23 * 02110-1301, USA. 24 */ 25 26 package org.jmol.script; 27 28 import java.util.Hashtable; 29 import java.util.Map; 30 31 import org.jmol.api.JmolParallelProcessor; 32 33 import javajs.util.SB; 34 35 public class ScriptContext { 36 37 private static int contextCount = 0; 38 39 private T[][] aatoken; 40 boolean allowJSThreads; 41 boolean chk; 42 public String contextPath = " >> "; 43 public Map<String, SV> vars; 44 boolean displayLoadErrorsSave; 45 public String errorMessage; 46 String errorMessageUntranslated; 47 public String errorType; 48 public boolean executionPaused; 49 public boolean executionStepping; 50 public String functionName; 51 public int iCommandError = -1; 52 public int id; 53 public boolean isComplete = true; 54 boolean isFunction; 55 public boolean isJSThread; 56 boolean isStateScript; 57 boolean isTryCatch; 58 SV[] forVars; 59 int iToken; 60 int lineEnd = Integer.MAX_VALUE; 61 public int[][] lineIndices; 62 short[] lineNumbers; 63 public boolean mustResumeEval; 64 public SB outputBuffer; 65 JmolParallelProcessor parallelProcessor; 66 public ScriptContext parentContext; 67 public int pc; 68 public int pc0; 69 public int pcEnd = Integer.MAX_VALUE; 70 public String script; 71 String scriptExtensions; 72 public String scriptFileName; 73 int scriptLevel; 74 public T[] statement; 75 Map<String, String> htFileCache; 76 int statementLength; 77 ContextToken token; 78 int tryPt; 79 T theToken; 80 int theTok; 81 82 private int[] pointers; 83 84 public String why; 85 86 public Map<String, ScriptFunction> privateFuncs; 87 ScriptContext()88 ScriptContext() { 89 id = ++contextCount; 90 } 91 setMustResume()92 public void setMustResume() { 93 ScriptContext sc = this; 94 while (sc != null) { 95 sc.mustResumeEval = true; 96 sc.pc = sc.pc0; 97 sc = sc.parentContext; 98 } 99 } 100 101 /** 102 * Context variables go up the stack until a 103 * function is found. That is considered to be 104 * the highest level. 105 * 106 * @param var 107 * @return context variables 108 */ getVariable(String var)109 public SV getVariable(String var) { 110 ScriptContext context = this; 111 while (context != null && !context.isFunction) { 112 if (context.vars != null 113 && context.vars.containsKey(var)) 114 return context.vars.get(var); 115 context = context.parentContext; 116 } 117 return null; 118 } 119 getFullMap()120 public Map<String, SV> getFullMap() { 121 Map<String, SV> ht = new Hashtable<String, SV>(); 122 ScriptContext context = this; 123 if (contextPath != null) 124 ht.put("_path", SV.newS(contextPath)); 125 while (context != null && !context.isFunction) { 126 if (context.vars != null) 127 for (String key : context.vars.keySet()) 128 if (!ht.containsKey(key)) { 129 SV val = context.vars.get(key); 130 if (val.tok != T.integer || val.intValue != Integer.MAX_VALUE) 131 ht.put(key, val); 132 } 133 context = context.parentContext; 134 } 135 return ht; 136 } 137 138 /** 139 * save pointers indicating state of if/then 140 * @param aa the command array token list 141 * 142 */ saveTokens(T[][] aa)143 void saveTokens(T[][] aa) { 144 aatoken = aa; 145 if (aa == null) { 146 pointers = null; 147 return; 148 } 149 pointers = new int[aa.length]; 150 for (int i = pointers.length; --i >= 0;) 151 pointers[i] = (aa[i] == null ? -1 : aa[i][0].intValue); 152 } 153 restoreTokens()154 T[][] restoreTokens() { 155 if (pointers != null) 156 for (int i = pointers.length; --i >= 0;) 157 if (aatoken[i] != null) 158 aatoken[i][0].intValue = pointers[i]; 159 return aatoken; 160 } 161 getTokenCount()162 public int getTokenCount() { 163 return (aatoken == null ? -1 : aatoken.length); 164 } 165 getToken(int i)166 public T[] getToken(int i) { 167 return aatoken[i]; 168 } 169 170 }