├── .gitattributes ├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE.txt ├── NOTICE-tools.txt ├── NOTICE.txt ├── README.md ├── RELEASE-NOTES.md ├── RELEASE-STEPS.md ├── build.gradle ├── checkstyle.xml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── man └── rhino.1 ├── resources ├── StrUtils.class ├── StrUtils.java └── dx-29.0.3.jar ├── spotbugs-exclude.xml ├── src ├── META-INF │ └── services │ │ └── javax.script.ScriptEngineFactory ├── defpackage │ └── StrUtils.java └── org │ └── mozilla │ ├── classfile │ ├── ByteCode.java │ ├── ClassFileField.java │ ├── ClassFileMethod.java │ ├── ClassFileWriter.java │ ├── ConstantEntry.java │ ├── ConstantPool.java │ ├── ExceptionTableEntry.java │ ├── FieldOrMethodRef.java │ ├── SuperBlock.java │ └── TypeInfo.java │ ├── javascript │ ├── AbstractEcmaObjectOperations.java │ ├── AccessorSlot.java │ ├── Arguments.java │ ├── ArrowFunction.java │ ├── BaseFunction.java │ ├── BoundFunction.java │ ├── Callable.java │ ├── ClassCache.java │ ├── ClassShutter.java │ ├── CodeGenerator.java │ ├── CompilerEnvirons.java │ ├── ConsString.java │ ├── ConstProperties.java │ ├── Constructable.java │ ├── Context.java │ ├── ContextAction.java │ ├── ContextFactory.java │ ├── ContextListener.java │ ├── ContinuationPending.java │ ├── DToA.java │ ├── Decompiler.java │ ├── DefaultErrorReporter.java │ ├── DefiningClassLoader.java │ ├── Delegator.java │ ├── ES6Generator.java │ ├── ES6Iterator.java │ ├── EcmaError.java │ ├── EmbeddedSlotMap.java │ ├── EqualObjectGraphs.java │ ├── ErrorReporter.java │ ├── Evaluator.java │ ├── EvaluatorException.java │ ├── ExternalArrayData.java │ ├── Function.java │ ├── FunctionObject.java │ ├── GeneratedClassLoader.java │ ├── HashSlotMap.java │ ├── Hashtable.java │ ├── IRFactory.java │ ├── Icode.java │ ├── IdFunctionCall.java │ ├── IdFunctionObject.java │ ├── IdFunctionObjectES6.java │ ├── IdScriptableObject.java │ ├── ImplementationVersion.java │ ├── ImporterTopLevel.java │ ├── InterfaceAdapter.java │ ├── InterpretedFunction.java │ ├── Interpreter.java │ ├── InterpreterData.java │ ├── IteratorLikeIterable.java │ ├── JavaAdapter.java │ ├── JavaMembers.java │ ├── JavaMembers_jdk11.java │ ├── JavaScriptException.java │ ├── JavaToJSONConverters.java │ ├── Kit.java │ ├── LambdaConstructor.java │ ├── LambdaFunction.java │ ├── LambdaSlot.java │ ├── LazilyLoadedCtor.java │ ├── LazyLoadSlot.java │ ├── MemberBox.java │ ├── NativeArray.java │ ├── NativeArrayIterator.java │ ├── NativeBigInt.java │ ├── NativeBoolean.java │ ├── NativeCall.java │ ├── NativeCallSite.java │ ├── NativeCollectionIterator.java │ ├── NativeContinuation.java │ ├── NativeDate.java │ ├── NativeError.java │ ├── NativeFunction.java │ ├── NativeGenerator.java │ ├── NativeGlobal.java │ ├── NativeIterator.java │ ├── NativeJSON.java │ ├── NativeJavaArray.java │ ├── NativeJavaClass.java │ ├── NativeJavaConstructor.java │ ├── NativeJavaList.java │ ├── NativeJavaMap.java │ ├── NativeJavaMethod.java │ ├── NativeJavaObject.java │ ├── NativeJavaPackage.java │ ├── NativeJavaTopPackage.java │ ├── NativeMap.java │ ├── NativeMath.java │ ├── NativeNumber.java │ ├── NativeObject.java │ ├── NativePromise.java │ ├── NativeScript.java │ ├── NativeSet.java │ ├── NativeString.java │ ├── NativeStringIterator.java │ ├── NativeSymbol.java │ ├── NativeWeakMap.java │ ├── NativeWeakSet.java │ ├── NativeWith.java │ ├── Node.java │ ├── NodeTransformer.java │ ├── ObjArray.java │ ├── ObjToIntMap.java │ ├── Parser.java │ ├── PolicySecurityController.java │ ├── Ref.java │ ├── RefCallable.java │ ├── RegExpProxy.java │ ├── RhinoException.java │ ├── RhinoSecurityManager.java │ ├── Script.java │ ├── ScriptRuntime.java │ ├── ScriptRuntimeES6.java │ ├── ScriptStackElement.java │ ├── Scriptable.java │ ├── ScriptableObject.java │ ├── SecureCaller.java │ ├── SecurityController.java │ ├── SecurityUtilities.java │ ├── Slot.java │ ├── SlotMap.java │ ├── SlotMapContainer.java │ ├── Sorting.java │ ├── SpecialRef.java │ ├── StackStyle.java │ ├── Symbol.java │ ├── SymbolKey.java │ ├── SymbolScriptable.java │ ├── Synchronizer.java │ ├── ThreadSafeSlotMapContainer.java │ ├── Token.java │ ├── TokenStream.java │ ├── TopLevel.java │ ├── UintMap.java │ ├── Undefined.java │ ├── UnhandledRejectionTracker.java │ ├── UniqueTag.java │ ├── VMBridge.java │ ├── WrapFactory.java │ ├── WrappedException.java │ ├── Wrapper.java │ ├── annotations │ │ ├── JSConstructor.java │ │ ├── JSFunction.java │ │ ├── JSGetter.java │ │ ├── JSSetter.java │ │ └── JSStaticFunction.java │ ├── ast │ │ ├── ArrayComprehension.java │ │ ├── ArrayComprehensionLoop.java │ │ ├── ArrayLiteral.java │ │ ├── Assignment.java │ │ ├── AstNode.java │ │ ├── AstRoot.java │ │ ├── BigIntLiteral.java │ │ ├── Block.java │ │ ├── BreakStatement.java │ │ ├── CatchClause.java │ │ ├── Comment.java │ │ ├── ConditionalExpression.java │ │ ├── ContinueStatement.java │ │ ├── DestructuringForm.java │ │ ├── DoLoop.java │ │ ├── ElementGet.java │ │ ├── EmptyExpression.java │ │ ├── EmptyStatement.java │ │ ├── ErrorCollector.java │ │ ├── ErrorNode.java │ │ ├── ExpressionStatement.java │ │ ├── ForInLoop.java │ │ ├── ForLoop.java │ │ ├── FunctionCall.java │ │ ├── FunctionNode.java │ │ ├── GeneratorExpression.java │ │ ├── GeneratorExpressionLoop.java │ │ ├── IdeErrorReporter.java │ │ ├── IfStatement.java │ │ ├── InfixExpression.java │ │ ├── Jump.java │ │ ├── KeywordLiteral.java │ │ ├── Label.java │ │ ├── LabeledStatement.java │ │ ├── LetNode.java │ │ ├── Loop.java │ │ ├── Name.java │ │ ├── NewExpression.java │ │ ├── NodeVisitor.java │ │ ├── NumberLiteral.java │ │ ├── ObjectLiteral.java │ │ ├── ObjectProperty.java │ │ ├── ParenthesizedExpression.java │ │ ├── ParseProblem.java │ │ ├── PropertyGet.java │ │ ├── RegExpLiteral.java │ │ ├── ReturnStatement.java │ │ ├── Scope.java │ │ ├── ScriptNode.java │ │ ├── StringLiteral.java │ │ ├── SwitchCase.java │ │ ├── SwitchStatement.java │ │ ├── Symbol.java │ │ ├── TaggedTemplateLiteral.java │ │ ├── TemplateCharacters.java │ │ ├── TemplateLiteral.java │ │ ├── ThrowStatement.java │ │ ├── TryStatement.java │ │ ├── UnaryExpression.java │ │ ├── UpdateExpression.java │ │ ├── VariableDeclaration.java │ │ ├── VariableInitializer.java │ │ ├── WhileLoop.java │ │ ├── WithStatement.java │ │ ├── XmlDotQuery.java │ │ ├── XmlElemRef.java │ │ ├── XmlExpression.java │ │ ├── XmlFragment.java │ │ ├── XmlLiteral.java │ │ ├── XmlMemberGet.java │ │ ├── XmlPropRef.java │ │ ├── XmlRef.java │ │ ├── XmlString.java │ │ └── Yield.java │ ├── commonjs │ │ └── module │ │ │ ├── ModuleScope.java │ │ │ ├── ModuleScript.java │ │ │ ├── ModuleScriptProvider.java │ │ │ ├── Require.java │ │ │ ├── RequireBuilder.java │ │ │ ├── package.html │ │ │ └── provider │ │ │ ├── CachingModuleScriptProviderBase.java │ │ │ ├── DefaultUrlConnectionExpiryCalculator.java │ │ │ ├── ModuleSource.java │ │ │ ├── ModuleSourceProvider.java │ │ │ ├── ModuleSourceProviderBase.java │ │ │ ├── MultiModuleScriptProvider.java │ │ │ ├── ParsedContentType.java │ │ │ ├── SoftCachingModuleScriptProvider.java │ │ │ ├── StrongCachingModuleScriptProvider.java │ │ │ ├── UrlConnectionExpiryCalculator.java │ │ │ ├── UrlConnectionSecurityDomainProvider.java │ │ │ ├── UrlModuleSourceProvider.java │ │ │ └── package.html │ ├── debug │ │ ├── DebugFrame.java │ │ ├── DebuggableObject.java │ │ ├── DebuggableScript.java │ │ └── Debugger.java │ ├── engine │ │ ├── BindingsObject.java │ │ ├── Builtins.java │ │ ├── RhinoCompiledScript.java │ │ ├── RhinoInvocationHandler.java │ │ ├── RhinoScriptEngine.java │ │ └── RhinoScriptEngineFactory.java │ ├── jdk18 │ │ └── VMBridge_jdk18.java │ ├── json │ │ └── JsonParser.java │ ├── optimizer │ │ ├── Block.java │ │ ├── BodyCodegen.java │ │ ├── ClassCompiler.java │ │ ├── Codegen.java │ │ ├── OptFunctionNode.java │ │ ├── OptRuntime.java │ │ ├── OptTransformer.java │ │ └── Optimizer.java │ ├── regexp │ │ ├── NativeRegExp.java │ │ ├── NativeRegExpCallable.java │ │ ├── NativeRegExpCtor.java │ │ ├── NativeRegExpInstantiator.java │ │ ├── RegExpImpl.java │ │ └── SubString.java │ ├── resources │ │ ├── Messages.properties │ │ ├── Messages_en.properties │ │ └── Messages_fr.properties │ ├── serialize │ │ ├── ScriptableInputStream.java │ │ └── ScriptableOutputStream.java │ ├── typedarrays │ │ ├── ByteIo.java │ │ ├── Conversions.java │ │ ├── NativeArrayBuffer.java │ │ ├── NativeArrayBufferView.java │ │ ├── NativeDataView.java │ │ ├── NativeFloat32Array.java │ │ ├── NativeFloat64Array.java │ │ ├── NativeInt16Array.java │ │ ├── NativeInt32Array.java │ │ ├── NativeInt8Array.java │ │ ├── NativeTypedArrayIterator.java │ │ ├── NativeTypedArrayView.java │ │ ├── NativeUint16Array.java │ │ ├── NativeUint32Array.java │ │ ├── NativeUint8Array.java │ │ └── NativeUint8ClampedArray.java │ ├── v8dtoa │ │ ├── CachedPowers.java │ │ ├── DiyFp.java │ │ ├── DoubleConversion.java │ │ ├── DoubleHelper.java │ │ ├── FastDtoa.java │ │ └── FastDtoaBuilder.java │ └── xml │ │ ├── XMLLib.java │ │ └── XMLObject.java │ └── mycode │ └── Main.java ├── tools └── ci │ └── linux │ ├── release.sh │ └── test.sh ├── toolsrc └── org │ └── mozilla │ └── javascript │ └── tools │ ├── SourceReader.java │ ├── ToolErrorReporter.java │ ├── debugger │ ├── Dim.java │ ├── GuiCallback.java │ ├── Main.java │ ├── ScopeProvider.java │ ├── SourceProvider.java │ ├── SwingGui.java │ ├── test.js │ └── treetable │ │ ├── AbstractCellEditor.java │ │ ├── JTreeTable.java │ │ ├── TreeTableModel.java │ │ └── TreeTableModelAdapter.java │ ├── jsc │ └── Main.java │ ├── resources │ └── Messages.properties │ └── shell │ ├── ConsoleTextArea.java │ ├── Environment.java │ ├── Global.java │ ├── JSConsole.java │ ├── JavaPolicySecurity.java │ ├── Main.java │ ├── QuitAction.java │ ├── SecurityProxy.java │ ├── ShellConsole.java │ ├── ShellContextFactory.java │ ├── ShellLine.java │ └── Timers.java └── xmlimplsrc └── org └── mozilla └── javascript └── xmlimpl ├── Namespace.java ├── QName.java ├── XML.java ├── XMLCtor.java ├── XMLLibImpl.java ├── XMLList.java ├── XMLName.java ├── XMLObjectImpl.java ├── XMLWithScope.java ├── XmlNode.java └── XmlProcessor.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set default behavior, in case users don't have core.autocrlf set. 2 | * text=auto 3 | 4 | .classpath text 5 | .project text 6 | *.html text 7 | *.htm text 8 | *.java text 9 | *.js text 10 | *.jsp text 11 | *.php text 12 | *.properties text 13 | *.sh text 14 | *.xml text 15 | *.md text 16 | *.bat text 17 | *.gradle text 18 | *.pl text 19 | *.mk text 20 | *.tests text 21 | *.doctest text 22 | /man/*.1 text 23 | /gradlew text 24 | 25 | *.bmp binary 26 | *.gif binary 27 | *.jar binary 28 | *.jpg binary 29 | *.jpeg binary 30 | *.png binary 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # os specific 2 | .DS_Store 3 | 4 | # build directories 5 | build/ 6 | lib/ 7 | buildGradle 8 | 9 | # artifacts 10 | *.zip 11 | 12 | *.iml 13 | .idea/ 14 | out/ 15 | target/ 16 | .gradle 17 | 18 | 19 | *~ 20 | 21 | # eclipse 22 | .classpath 23 | .project 24 | test262 25 | bin/ 26 | .settings/ 27 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Community Participation Guidelines 2 | 3 | This repository is governed by Mozilla's code of conduct and etiquette guidelines. 4 | For more details, please read the 5 | [Mozilla Community Participation Guidelines](https://www.mozilla.org/about/governance/policies/participation/). 6 | 7 | ## How to Report 8 | For more information on how to report violations of the Community Participation Guidelines, please read our '[How to Report](https://www.mozilla.org/about/governance/policies/participation/reporting/)' page. 9 | 10 | 16 | -------------------------------------------------------------------------------- /NOTICE-tools.txt: -------------------------------------------------------------------------------- 1 | Rhino is licensed subject to the terms of the Mozilla Public License, v. 2.0. 2 | See "LICENSE.txt" for the text of the license. 3 | 4 | The files in toolsrc/org/mozilla/javascript/tools/debugger/treetable (runtime 5 | package org.mozilla.javascript.tools.debugger.treetable) are available under 6 | the following license: 7 | 8 | ---- 9 | 10 | Copyright 1997, 1998 Sun Microsystems, Inc. All Rights Reserved. 11 | 12 | Redistribution and use in source and binary forms, with or without 13 | modification, are permitted provided that the following conditions 14 | are met: 15 | - Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | 18 | - Redistributions in binary form must reproduce the above copyright 19 | notice, this list of conditions and the following disclaimer in the 20 | documentation and/or other materials provided with the distribution. 21 | 22 | - Neither the name of Sun Microsystems nor the names of its 23 | contributors may be used to endorse or promote products derived 24 | from this software without specific prior written permission. 25 | 26 | 27 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 28 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 29 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 31 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 32 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 33 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 34 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 35 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 36 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 37 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 | -------------------------------------------------------------------------------- /NOTICE.txt: -------------------------------------------------------------------------------- 1 | Rhino is licensed subject to the terms of the Mozilla Public License, v. 2.0. 2 | See "License.txt" for the text of the license. 3 | 4 | Rhino contains the following additional software: 5 | 6 | ---- 7 | 8 | Portions of the floating-point conversion code, and portions of the test suite 9 | come from the Google V8 JavaScript engine and are subject to the following: 10 | 11 | Copyright 2010-2015 the V8 project authors. All rights reserved. 12 | Redistribution and use in source and binary forms, with or without 13 | modification, are permitted provided that the following conditions are 14 | met: 15 | 16 | * Redistributions of source code must retain the above copyright 17 | notice, this list of conditions and the following disclaimer. 18 | * Redistributions in binary form must reproduce the above 19 | copyright notice, this list of conditions and the following 20 | disclaimer in the documentation and/or other materials provided 21 | with the distribution. 22 | * Neither the name of Google Inc. nor the names of its 23 | contributors may be used to endorse or promote products derived 24 | from this software without specific prior written permission. 25 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /RELEASE-STEPS.md: -------------------------------------------------------------------------------- 1 | # Rhino Release Process 2 | 3 | ## Prepare the repository 4 | 5 | Update the version in gradle.properties to the new release version. 6 | 7 | Update RELEASE_NOTES.md to include at least a summary of the major features 8 | of the release. (This is historical, but many find it helpful to have a single 9 | file in the repo that lists the major changes.) 10 | 11 | Update README.md to add a row to the table of releases that points to the 12 | GitHub release that we'll create in the next step (just follow the pattern 13 | that's there). 14 | 15 | Now might be a good time to run "./gradlew publishToMavenLocal" and use the 16 | published JARs as a sanity check. 17 | 18 | ## Update Compatibility Table 19 | 20 | The offial Kangax "compat table" now supports Rhino, but it's convenient 21 | to have our own that shows progress across all releases. Here's how to 22 | update it: 23 | 24 | git clone -b gh-pages https://github.com/gbrail/node-compat-table.git 25 | cd node-compat-table 26 | 27 | Now, edit "rhinoall.sh" to include the new release -- it includes a series 28 | of lines that fetch old releases, and use your local build of the new one. 29 | Then, update the table: 30 | 31 | ./rhinoall.sh 32 | 33 | The resulting "index.html" can be copied into "docs/compat/engines.html" in 34 | this repo. 35 | 36 | ## Push the Release to GitHub 37 | 38 | At this point, the current contents of your directory correspond to the 39 | new release. Prepare a pull request containing the changes, submit it, 40 | and merge it -- the result will be that the head of the "master" branch 41 | will build your new release. 42 | 43 | Update to that branch and create a tag for the release, where XX is a number 44 | like "1_7_14": 45 | 46 | git pull origin master 47 | git tag Rhino_XX_Release 48 | git push origin Rhino_XX_Release 49 | 50 | Now, on the Rhino "Releases" tab in GitHub, create a release that corresponds 51 | to the new tag. Include the following: 52 | 53 | * A cut and paste of the part of RELEASE_NOTES.md added for the release 54 | * The three JARs created by "./gradlew.jar" 55 | * The ZIP file created by "./gradlew distZip" 56 | * A ZIP of the source will be included automatically by GitHub 57 | 58 | ## Push the release to Maven Central 59 | 60 | The "Publish to Maven Central" action on GitHub Actions will automatically 61 | build the release, sign the JARs, and push it to oss.sonatype.org in the 62 | "org.mozilla" area. Log in to oss.sonatype.org, verify that all the checks 63 | that happen there were successful, and "close" the release. It will appear 64 | on Maven Central a few hours later. 65 | 66 | ## Update Homebrew 67 | 68 | The Homebrew team for Mac does not necessarily pick up Rhino releases 69 | automatically. It may be necessary to submit a PR to the "homebrew/homebrew" 70 | repo in GitHub for a change to the file "Library/Formula/rhino.rb". 71 | 72 | ## Prepare for Next Release 73 | 74 | Now it's time to move to the next "SNAPSHOT" release. Update gradle.properties, 75 | create a PR, and push the new PR. Now development can proceeed anew! 76 | 77 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | rootProject.name=rhino 2 | group=org.mozilla 3 | version=1.7.14 4 | buildDir=buildGradle 5 | mavenSnapshotRepo=https://oss.sonatype.org/content/repositories/snapshots 6 | mavenReleaseRepo=https://oss.sonatype.org/service/local/staging/deploy/maven2/ 7 | org.gradle.caching=true 8 | org.gradle.parallel=true 9 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/autox-community/Autojs_Rhino_Dex/245911092fae9bef524fb1ab48e0f20720e39414/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-7.2-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/autox-community/Autojs_Rhino_Dex/245911092fae9bef524fb1ab48e0f20720e39414/gradlew -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /resources/StrUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/autox-community/Autojs_Rhino_Dex/245911092fae9bef524fb1ab48e0f20720e39414/resources/StrUtils.class -------------------------------------------------------------------------------- /resources/StrUtils.java: -------------------------------------------------------------------------------- 1 | package defpackage; 2 | 3 | public class StrUtils { 4 | public static String d(String data) { 5 | int l = data.length() / 2; 6 | byte[] b = new byte[l]; 7 | for (int i = 0; i < l; i++) { 8 | b[i] = Integer.valueOf(data.substring(i * 2, (i * 2) + 2), 16).byteValue(); 9 | } 10 | for (int i2 = 0; i2 < b.length; i2++) { 11 | b[i2] = (byte) (b[i2] - 1); 12 | } 13 | return new String(b); 14 | } 15 | } -------------------------------------------------------------------------------- /resources/dx-29.0.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/autox-community/Autojs_Rhino_Dex/245911092fae9bef524fb1ab48e0f20720e39414/resources/dx-29.0.3.jar -------------------------------------------------------------------------------- /src/META-INF/services/javax.script.ScriptEngineFactory: -------------------------------------------------------------------------------- 1 | org.mozilla.javascript.engine.RhinoScriptEngineFactory 2 | -------------------------------------------------------------------------------- /src/defpackage/StrUtils.java: -------------------------------------------------------------------------------- 1 | package defpackage; 2 | 3 | import org.mozilla.classfile.ByteCode; 4 | import org.mozilla.classfile.ClassFileWriter; 5 | 6 | import static org.mozilla.mycode.Main._isNoEncryptString; 7 | 8 | public class StrUtils { 9 | 10 | /** 11 | * 字符串加密 12 | */ 13 | public static String e(String data) { 14 | byte[] b = data.getBytes(); 15 | for (int i = 0; i < b.length; i++) { 16 | b[i] = (byte) (b[i] + 1); 17 | } 18 | StringBuilder hex = new StringBuilder(); 19 | for (int i2 = 0; i2 < b.length; i2++) { 20 | hex.append(String.format("%02X", Byte.valueOf(b[i2]))); 21 | } 22 | return hex.toString(); 23 | } 24 | 25 | /** 26 | * 字符串解密 27 | */ 28 | public static String d(String data) { 29 | int l = data.length() / 2; 30 | byte[] b = new byte[l]; 31 | for (int i = 0; i < l; i++) { 32 | b[i] = Integer.valueOf(data.substring(i * 2, (i * 2) + 2), 16).byteValue(); 33 | } 34 | for (int i2 = 0; i2 < b.length; i2++) { 35 | b[i2] = (byte) (b[i2] - 1); 36 | } 37 | return new String(b); 38 | } 39 | 40 | /** 41 | * 加密,把一个字符串在原有的基础上 加一个数值 42 | */ 43 | public static String e2(String data) { 44 | //把字符串转为字节数组 45 | byte[] b = data.getBytes(); 46 | //遍历 47 | for (int i = 0; i < b.length; i++) { 48 | b[i] += 3;//在原有的基础上 加一个数值 49 | } 50 | return new String(b); 51 | } 52 | 53 | /** 54 | * 解密:把一个加密后的字符串在原有基础上 减一个数 55 | * 56 | * @param data 加密后的字符串 57 | * @return 返回解密后的新字符串 58 | */ 59 | public static String d2(String data) { 60 | //把字符串转为字节数组 61 | byte[] b = data.getBytes(); 62 | //遍历 63 | for (int i = 0; i < b.length; i++) { 64 | b[i] -= 3;//在原有的基础上 减一个数 65 | } 66 | return new String(b); 67 | } 68 | 69 | /** 70 | * 字符串加密 71 | */ 72 | public static void strE(ClassFileWriter cfw, String content) { 73 | // 禁用加密 74 | if (_isNoEncryptString) { 75 | cfw.addPush(content); 76 | return; 77 | } 78 | if (content.isEmpty()) { 79 | //为空 则 不调用 解密.. 80 | cfw.addPush(content); 81 | } else { 82 | cfw.addPush(StrUtils.e(content)); 83 | cfw.addInvoke(ByteCode.INVOKESTATIC, "defpackage.StrUtils", "d", "(Ljava/lang/String;)" + 84 | "Ljava/lang/String;"); 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /src/org/mozilla/classfile/ClassFileField.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.classfile; 8 | 9 | final class ClassFileField 10 | { 11 | ClassFileField(short nameIndex, short typeIndex, short flags) 12 | { 13 | itsNameIndex = nameIndex; 14 | itsTypeIndex = typeIndex; 15 | itsFlags = flags; 16 | itsHasAttributes = false; 17 | } 18 | 19 | void setAttributes(short attr1, short attr2, short attr3, int index) 20 | { 21 | itsHasAttributes = true; 22 | itsAttr1 = attr1; 23 | itsAttr2 = attr2; 24 | itsAttr3 = attr3; 25 | itsIndex = index; 26 | } 27 | 28 | int write(byte[] data, int offset) 29 | { 30 | offset = ClassFileWriter.putInt16(itsFlags, data, offset); 31 | offset = ClassFileWriter.putInt16(itsNameIndex, data, offset); 32 | offset = ClassFileWriter.putInt16(itsTypeIndex, data, offset); 33 | if (!itsHasAttributes) { 34 | // write 0 attributes 35 | offset = ClassFileWriter.putInt16(0, data, offset); 36 | } else { 37 | offset = ClassFileWriter.putInt16(1, data, offset); 38 | offset = ClassFileWriter.putInt16(itsAttr1, data, offset); 39 | offset = ClassFileWriter.putInt16(itsAttr2, data, offset); 40 | offset = ClassFileWriter.putInt16(itsAttr3, data, offset); 41 | offset = ClassFileWriter.putInt16(itsIndex, data, offset); 42 | } 43 | return offset; 44 | } 45 | 46 | int getWriteSize() 47 | { 48 | int size = 2 * 3; 49 | if (!itsHasAttributes) { 50 | size += 2; 51 | } else { 52 | size += 2 + 2 * 4; 53 | } 54 | return size; 55 | } 56 | 57 | private short itsNameIndex; 58 | private short itsTypeIndex; 59 | private short itsFlags; 60 | private boolean itsHasAttributes; 61 | private short itsAttr1, itsAttr2, itsAttr3; 62 | private int itsIndex; 63 | } -------------------------------------------------------------------------------- /src/org/mozilla/classfile/ClassFileMethod.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.classfile; 8 | 9 | final class ClassFileMethod 10 | { 11 | ClassFileMethod(String name, short nameIndex, String type, short typeIndex, 12 | short flags) 13 | { 14 | itsName = name; 15 | itsNameIndex = nameIndex; 16 | itsType = type; 17 | itsTypeIndex = typeIndex; 18 | itsFlags = flags; 19 | } 20 | 21 | void setCodeAttribute(byte codeAttribute[]) 22 | { 23 | itsCodeAttribute = codeAttribute; 24 | } 25 | 26 | int write(byte[] data, int offset) 27 | { 28 | offset = ClassFileWriter.putInt16(itsFlags, data, offset); 29 | offset = ClassFileWriter.putInt16(itsNameIndex, data, offset); 30 | offset = ClassFileWriter.putInt16(itsTypeIndex, data, offset); 31 | // Code attribute only 32 | offset = ClassFileWriter.putInt16(1, data, offset); 33 | System.arraycopy(itsCodeAttribute, 0, data, offset, 34 | itsCodeAttribute.length); 35 | offset += itsCodeAttribute.length; 36 | return offset; 37 | } 38 | 39 | int getWriteSize() 40 | { 41 | return 2 * 4 + itsCodeAttribute.length; 42 | } 43 | 44 | String getName() 45 | { 46 | return itsName; 47 | } 48 | 49 | String getType() 50 | { 51 | return itsType; 52 | } 53 | 54 | short getFlags() 55 | { 56 | return itsFlags; 57 | } 58 | 59 | private String itsName; 60 | private String itsType; 61 | private short itsNameIndex; 62 | private short itsTypeIndex; 63 | private short itsFlags; 64 | private byte[] itsCodeAttribute; 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/org/mozilla/classfile/ConstantEntry.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.classfile; 8 | 9 | final class ConstantEntry { 10 | private int type; 11 | private int intval; 12 | private long longval; 13 | private String str1; 14 | private String str2; 15 | private int hashcode; 16 | 17 | ConstantEntry(int type, int intval, String str1, String str2) { 18 | this.type = type; 19 | this.intval = intval; 20 | this.str1 = str1; 21 | this.str2 = str2; 22 | hashcode = type ^ intval + str1.hashCode() * str2.hashCode(); 23 | } 24 | 25 | @Override 26 | public int hashCode() { 27 | return hashcode; 28 | } 29 | 30 | @Override 31 | public boolean equals(Object obj) { 32 | if (!(obj instanceof ConstantEntry)) { 33 | return false; 34 | } 35 | ConstantEntry entry = (ConstantEntry) obj; 36 | if (type != entry.type) { 37 | return false; 38 | } 39 | switch (type) { 40 | case ConstantPool.CONSTANT_Integer: 41 | case ConstantPool.CONSTANT_Float: 42 | return intval == entry.intval; 43 | case ConstantPool.CONSTANT_Long: 44 | case ConstantPool.CONSTANT_Double: 45 | return longval == entry.longval; 46 | case ConstantPool.CONSTANT_NameAndType: 47 | return str1.equals(entry.str1) && str2.equals(entry.str2); 48 | case ConstantPool.CONSTANT_InvokeDynamic: 49 | return intval == entry.intval 50 | && str1.equals(entry.str1) 51 | && str2.equals(entry.str2); 52 | default: 53 | throw new RuntimeException("unsupported constant type"); 54 | 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/org/mozilla/classfile/ExceptionTableEntry.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.classfile; 8 | 9 | final class ExceptionTableEntry 10 | { 11 | ExceptionTableEntry(int startLabel, int endLabel, 12 | int handlerLabel, short catchType) 13 | { 14 | itsStartLabel = startLabel; 15 | itsEndLabel = endLabel; 16 | itsHandlerLabel = handlerLabel; 17 | itsCatchType = catchType; 18 | } 19 | 20 | int itsStartLabel; 21 | int itsEndLabel; 22 | int itsHandlerLabel; 23 | short itsCatchType; 24 | } 25 | -------------------------------------------------------------------------------- /src/org/mozilla/classfile/FieldOrMethodRef.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.classfile; 8 | 9 | final class FieldOrMethodRef 10 | { 11 | FieldOrMethodRef(String className, String name, String type) 12 | { 13 | this.className = className; 14 | this.name = name; 15 | this.type = type; 16 | } 17 | 18 | public String getClassName() 19 | { 20 | return className; 21 | } 22 | 23 | public String getName() 24 | { 25 | return name; 26 | } 27 | 28 | public String getType() 29 | { 30 | return type; 31 | } 32 | 33 | @Override 34 | public boolean equals(Object obj) 35 | { 36 | if (!(obj instanceof FieldOrMethodRef)) { return false; } 37 | FieldOrMethodRef x = (FieldOrMethodRef)obj; 38 | return className.equals(x.className) 39 | && name.equals(x.name) 40 | && type.equals(x.type); 41 | } 42 | 43 | @Override 44 | public int hashCode() 45 | { 46 | if (hashCode == -1) { 47 | int h1 = className.hashCode(); 48 | int h2 = name.hashCode(); 49 | int h3 = type.hashCode(); 50 | hashCode = h1 ^ h2 ^ h3; 51 | } 52 | return hashCode; 53 | } 54 | 55 | private String className; 56 | private String name; 57 | private String type; 58 | private int hashCode = -1; 59 | } 60 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/Callable.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | /** 10 | * Generic notion of callable object that can execute some script-related code 11 | * upon request with specified values for script scope and this objects. 12 | */ 13 | public interface Callable 14 | { 15 | /** 16 | * Perform the call. 17 | * 18 | * @param cx the current Context for this thread 19 | * @param scope the scope to use to resolve properties. 20 | * @param thisObj the JavaScript this object 21 | * @param args the array of arguments 22 | * @return the result of the call 23 | */ 24 | public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ClassShutter.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript; 10 | 11 | /** 12 | Embeddings that wish to filter Java classes that are visible to scripts 13 | through the LiveConnect, should implement this interface. 14 | 15 | @see Context#setClassShutter(ClassShutter) 16 | @since 1.5 Release 4 17 | @author Norris Boyd 18 | */ 19 | 20 | public interface ClassShutter { 21 | 22 | /** 23 | * Return true iff the Java class with the given name should be exposed 24 | * to scripts. 25 | *

26 | * An embedding may filter which Java classes are exposed through 27 | * LiveConnect to JavaScript scripts. 28 | *

29 | * Due to the fact that there is no package reflection in Java, 30 | * this method will also be called with package names. There 31 | * is no way for Rhino to tell if "Packages.a.b" is a package name 32 | * or a class that doesn't exist. What Rhino does is attempt 33 | * to load each segment of "Packages.a.b.c": It first attempts to 34 | * load class "a", then attempts to load class "a.b", then 35 | * finally attempts to load class "a.b.c". On a Rhino installation 36 | * without any ClassShutter set, and without any of the 37 | * above classes, the expression "Packages.a.b.c" will result in 38 | * a [JavaPackage a.b.c] and not an error. 39 | *

40 | * With ClassShutter supplied, Rhino will first call 41 | * visibleToScripts before attempting to look up the class name. If 42 | * visibleToScripts returns false, the class name lookup is not 43 | * performed and subsequent Rhino execution assumes the class is 44 | * not present. So for "java.lang.System.out.println" the lookup 45 | * of "java.lang.System" is skipped and thus Rhino assumes that 46 | * "java.lang.System" doesn't exist. So then for "java.lang.System.out", 47 | * Rhino attempts to load the class "java.lang.System.out" because 48 | * it assumes that "java.lang.System" is a package name. 49 | *

50 | * @param fullClassName the full name of the class (including the package 51 | * name, with '.' as a delimiter). For example the 52 | * standard string class is "java.lang.String" 53 | * @return whether or not to reveal this class to scripts 54 | */ 55 | public boolean visibleToScripts(String fullClassName); 56 | } 57 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/Constructable.java: -------------------------------------------------------------------------------- 1 | package org.mozilla.javascript; 2 | 3 | /** An interface that can be used to implement a constructor function as a lambda. */ 4 | public interface Constructable { 5 | 6 | /** 7 | * Call the function as a constructor. 8 | * 9 | *

This method is invoked by the runtime in order to satisfy a use of the JavaScript 10 | * new operator. This method is expected to create a new object and return it. 11 | * 12 | * @param cx the current Context for this thread 13 | * @param scope an enclosing scope of the caller except when the function is called from a 14 | * closure. 15 | * @param args the array of arguments 16 | * @return the allocated object 17 | */ 18 | Scriptable construct(Context cx, Scriptable scope, Object[] args); 19 | } 20 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ContextAction.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript; 10 | 11 | /** 12 | * Interface to represent arbitrary action that requires to have Context 13 | * object associated with the current thread for its execution. 14 | * @param T the type of the return value of action execution 15 | */ 16 | public interface ContextAction 17 | { 18 | /** 19 | * Execute action using the supplied Context instance. 20 | * When Rhino runtime calls the method, cx will be associated 21 | * with the current thread as active context. 22 | * 23 | * @see ContextFactory#call(ContextAction) 24 | */ 25 | public T run(Context cx); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ContextListener.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript; 10 | 11 | /** 12 | * @deprecated Embeddings that wish to customize newly created 13 | * {@link Context} instances should implement 14 | * {@link ContextFactory.Listener}. 15 | */ 16 | @Deprecated 17 | public interface ContextListener extends ContextFactory.Listener 18 | { 19 | 20 | /** 21 | * @deprecated Rhino runtime never calls the method. 22 | */ 23 | @Deprecated 24 | public void contextEntered(Context cx); 25 | 26 | /** 27 | * @deprecated Rhino runtime never calls the method. 28 | */ 29 | @Deprecated 30 | public void contextExited(Context cx); 31 | } 32 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/DefiningClassLoader.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript; 6 | 7 | /** 8 | * Load generated classes. 9 | * 10 | * @author Norris Boyd 11 | */ 12 | public class DefiningClassLoader extends ClassLoader 13 | implements GeneratedClassLoader 14 | { 15 | public DefiningClassLoader() { 16 | this.parentLoader = getClass().getClassLoader(); 17 | } 18 | 19 | public DefiningClassLoader(ClassLoader parentLoader) { 20 | this.parentLoader = parentLoader; 21 | } 22 | 23 | @Override 24 | public Class defineClass(String name, byte[] data) { 25 | // Use our own protection domain for the generated classes. 26 | // TODO: we might want to use a separate protection domain for classes 27 | // compiled from scripts, based on where the script was loaded from. 28 | return super.defineClass(name, data, 0, data.length, 29 | SecurityUtilities.getProtectionDomain(getClass())); 30 | } 31 | 32 | @Override 33 | public void linkClass(Class cl) { 34 | resolveClass(cl); 35 | } 36 | 37 | @Override 38 | public Class loadClass(String name, boolean resolve) 39 | throws ClassNotFoundException 40 | { 41 | Class cl = findLoadedClass(name); 42 | if (cl == null) { 43 | if (parentLoader != null) { 44 | cl = parentLoader.loadClass(name); 45 | } else { 46 | cl = findSystemClass(name); 47 | } 48 | } 49 | if (resolve) { 50 | resolveClass(cl); 51 | } 52 | return cl; 53 | } 54 | 55 | private final ClassLoader parentLoader; 56 | } 57 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ErrorReporter.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript; 10 | 11 | /** 12 | * This is interface defines a protocol for the reporting of 13 | * errors during JavaScript translation or execution. 14 | * 15 | * @author Norris Boyd 16 | */ 17 | 18 | public interface ErrorReporter { 19 | 20 | /** 21 | * Report a warning. 22 | * 23 | * The implementing class may choose to ignore the warning 24 | * if it desires. 25 | * 26 | * @param message a String describing the warning 27 | * @param sourceName a String describing the JavaScript source 28 | * where the warning occured; typically a filename or URL 29 | * @param line the line number associated with the warning 30 | * @param lineSource the text of the line (may be null) 31 | * @param lineOffset the offset into lineSource where problem was detected 32 | */ 33 | void warning(String message, String sourceName, int line, 34 | String lineSource, int lineOffset); 35 | 36 | /** 37 | * Report an error. 38 | * 39 | * The implementing class is free to throw an exception if 40 | * it desires. 41 | * 42 | * If execution has not yet begun, the JavaScript engine is 43 | * free to find additional errors rather than terminating 44 | * the translation. It will not execute a script that had 45 | * errors, however. 46 | * 47 | * @param message a String describing the error 48 | * @param sourceName a String describing the JavaScript source 49 | * where the error occured; typically a filename or URL 50 | * @param line the line number associated with the error 51 | * @param lineSource the text of the line (may be null) 52 | * @param lineOffset the offset into lineSource where problem was detected 53 | */ 54 | void error(String message, String sourceName, int line, 55 | String lineSource, int lineOffset); 56 | 57 | /** 58 | * Creates an EvaluatorException that may be thrown. 59 | * 60 | * runtimeErrors, unlike errors, will always terminate the 61 | * current script. 62 | * 63 | * @param message a String describing the error 64 | * @param sourceName a String describing the JavaScript source 65 | * where the error occured; typically a filename or URL 66 | * @param line the line number associated with the error 67 | * @param lineSource the text of the line (may be null) 68 | * @param lineOffset the offset into lineSource where problem was detected 69 | * @return an EvaluatorException that will be thrown. 70 | */ 71 | EvaluatorException runtimeError(String message, String sourceName, 72 | int line, String lineSource, 73 | int lineOffset); 74 | } 75 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/EvaluatorException.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | 8 | package org.mozilla.javascript; 9 | 10 | /** 11 | * The class of exceptions thrown by the JavaScript engine. 12 | */ 13 | public class EvaluatorException extends RhinoException 14 | { 15 | private static final long serialVersionUID = -8743165779676009808L; 16 | 17 | public EvaluatorException(String detail) 18 | { 19 | super(detail); 20 | } 21 | 22 | /** 23 | * Create an exception with the specified detail message. 24 | * 25 | * Errors internal to the JavaScript engine will simply throw a 26 | * RuntimeException. 27 | * 28 | * @param detail the error message 29 | * @param sourceName the name of the source reponsible for the error 30 | * @param lineNumber the line number of the source 31 | */ 32 | public EvaluatorException(String detail, String sourceName, 33 | int lineNumber) 34 | { 35 | this(detail, sourceName, lineNumber, null, 0); 36 | } 37 | 38 | /** 39 | * Create an exception with the specified detail message. 40 | * 41 | * Errors internal to the JavaScript engine will simply throw a 42 | * RuntimeException. 43 | * 44 | * @param detail the error message 45 | * @param sourceName the name of the source responsible for the error 46 | * @param lineNumber the line number of the source 47 | * @param columnNumber the columnNumber of the source (may be zero if 48 | * unknown) 49 | * @param lineSource the source of the line containing the error (may be 50 | * null if unknown) 51 | */ 52 | public EvaluatorException(String detail, String sourceName, int lineNumber, 53 | String lineSource, int columnNumber) 54 | { 55 | super(detail); 56 | recordErrorOrigin(sourceName, lineNumber, lineSource, columnNumber); 57 | } 58 | 59 | /** 60 | * @deprecated Use {@link RhinoException#sourceName()} from the super class. 61 | */ 62 | @Deprecated 63 | public String getSourceName() 64 | { 65 | return sourceName(); 66 | } 67 | 68 | /** 69 | * @deprecated Use {@link RhinoException#lineNumber()} from the super class. 70 | */ 71 | @Deprecated 72 | public int getLineNumber() 73 | { 74 | return lineNumber(); 75 | } 76 | 77 | /** 78 | * @deprecated Use {@link RhinoException#columnNumber()} from the super class. 79 | */ 80 | @Deprecated 81 | public int getColumnNumber() 82 | { 83 | return columnNumber(); 84 | } 85 | 86 | /** 87 | * @deprecated Use {@link RhinoException#lineSource()} from the super class. 88 | */ 89 | @Deprecated 90 | public String getLineSource() 91 | { 92 | return lineSource(); 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ExternalArrayData.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | /** 10 | * Implement this interface in order to allow external data to be attached to a ScriptableObject. 11 | */ 12 | 13 | public interface ExternalArrayData 14 | { 15 | /** 16 | * Return the element at the specified index. The result must be a type that is valid in JavaScript: 17 | * Number, String, or Scriptable. This method will not be called unless "index" is in 18 | * range. 19 | */ 20 | Object getArrayElement(int index); 21 | 22 | /** 23 | * Set the element at the specified index. This method will not be called unless "index" is in 24 | * range. The method must check that "value" is a valid type, and convert it if necessary. 25 | */ 26 | void setArrayElement(int index, Object value); 27 | 28 | /** 29 | * Return the length of the array. 30 | */ 31 | int getArrayLength(); 32 | } 33 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/Function.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript; 10 | 11 | /** 12 | * This is interface that all functions in JavaScript must implement. The interface provides for 13 | * calling functions and constructors. 14 | * 15 | * @see org.mozilla.javascript.Scriptable 16 | * @author Norris Boyd 17 | */ 18 | public interface Function extends Scriptable, Callable, Constructable { 19 | /** 20 | * Call the function. 21 | * 22 | *

Note that the array of arguments is not guaranteed to have length greater than 0. 23 | * 24 | * @param cx the current Context for this thread 25 | * @param scope the scope to execute the function relative to. This is set to the value returned 26 | * by getParentScope() except when the function is called from a closure. 27 | * @param thisObj the JavaScript this object 28 | * @param args the array of arguments 29 | * @return the result of the call 30 | */ 31 | @Override 32 | Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args); 33 | 34 | /** 35 | * Call the function as a constructor. 36 | * 37 | *

This method is invoked by the runtime in order to satisfy a use of the JavaScript 38 | * new operator. This method is expected to create a new object and return it. 39 | * 40 | * @param cx the current Context for this thread 41 | * @param scope an enclosing scope of the caller except when the function is called from a 42 | * closure. 43 | * @param args the array of arguments 44 | * @return the allocated object 45 | */ 46 | @Override 47 | Scriptable construct(Context cx, Scriptable scope, Object[] args); 48 | } 49 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/GeneratedClassLoader.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript; 10 | 11 | /** 12 | * Interface to define classes from generated byte code. 13 | */ 14 | public interface GeneratedClassLoader { 15 | 16 | /** 17 | * Define a new Java class. 18 | * Classes created via this method should have the same class loader. 19 | * 20 | * @param name fully qualified class name 21 | * @param data class byte code 22 | * @return new class object 23 | */ 24 | public Class defineClass(String name, byte[] data); 25 | 26 | /** 27 | * Link the given class. 28 | * 29 | * @param cl Class instance returned from the previous call to 30 | * {@link #defineClass(String, byte[])} 31 | * @see java.lang.ClassLoader 32 | */ 33 | public void linkClass(Class cl); 34 | } 35 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/IdFunctionCall.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | /** 10 | * Master for id-based functions that knows their properties and how to 11 | * execute them. 12 | */ 13 | public interface IdFunctionCall 14 | { 15 | /** 16 | * 'thisObj' will be null if invoked as constructor, in which case 17 | * instance of Scriptable should be returned 18 | */ 19 | public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, 20 | Scriptable thisObj, Object[] args); 21 | 22 | } 23 | 24 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/IdFunctionObjectES6.java: -------------------------------------------------------------------------------- 1 | package org.mozilla.javascript; 2 | 3 | public class IdFunctionObjectES6 extends IdFunctionObject{ 4 | 5 | private static final long serialVersionUID = -8023088662589035261L; 6 | 7 | public IdFunctionObjectES6(IdFunctionCall idcall, Object tag, int id, String name, int arity, Scriptable scope) { 8 | super(idcall, tag, id, name, arity, scope); 9 | } 10 | 11 | private static final int Id_length = 1, Id_name = 3; 12 | private boolean myLength = true; 13 | private boolean myName = true; 14 | 15 | @Override 16 | protected int findInstanceIdInfo(String s) { 17 | if (s.equals("length")) return instanceIdInfo(READONLY | DONTENUM, Id_length); 18 | else if (s.equals("name")) return instanceIdInfo(READONLY | DONTENUM, Id_name); 19 | return super.findInstanceIdInfo(s); 20 | } 21 | 22 | @Override 23 | protected Object getInstanceIdValue(int id) { 24 | if (id == Id_length && !myLength) { 25 | return NOT_FOUND; 26 | } else if (id == Id_name && !myName) { 27 | return NOT_FOUND; 28 | } 29 | return super.getInstanceIdValue(id); 30 | } 31 | 32 | @Override 33 | protected void setInstanceIdValue(int id, Object value) { 34 | if (id == Id_length && value == NOT_FOUND) { 35 | this.myLength = false; 36 | return; 37 | } else if (id == Id_name && value == NOT_FOUND) { 38 | this.myName = false; 39 | return; 40 | } 41 | super.setInstanceIdValue(id, value); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ImplementationVersion.java: -------------------------------------------------------------------------------- 1 | package org.mozilla.javascript; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.net.URL; 6 | import java.util.Enumeration; 7 | import java.util.jar.Attributes; 8 | import java.util.jar.Manifest; 9 | 10 | /** 11 | * This class is a singleton that just exists to serve up the implementation version. This should 12 | * encourage that it's safely but lazily loaded just once per VM. 13 | */ 14 | public class ImplementationVersion { 15 | 16 | private String versionString; 17 | 18 | private static final ImplementationVersion version = new ImplementationVersion(); 19 | 20 | public static String get() { 21 | return version.versionString; 22 | } 23 | 24 | private ImplementationVersion() { 25 | Enumeration urls; 26 | try { 27 | urls = 28 | ImplementationVersion.class 29 | .getClassLoader() 30 | .getResources("META-INF/MANIFEST.MF"); 31 | } catch (IOException ioe) { 32 | return; 33 | } 34 | 35 | // There will be many manifests in the world -- enumerate all of them until we find the 36 | // right one. 37 | while (urls.hasMoreElements()) { 38 | URL metaUrl = urls.nextElement(); 39 | try (InputStream is = metaUrl.openStream()) { 40 | Manifest mf = new Manifest(is); 41 | Attributes attrs = mf.getMainAttributes(); 42 | if ("Mozilla Rhino".equals(attrs.getValue("Implementation-Title"))) { 43 | StringBuilder buf = new StringBuilder(23); 44 | buf.append("Rhino ").append(attrs.getValue("Implementation-Version")); 45 | String builtDate = attrs.getValue("Built-Date"); 46 | if (builtDate != null) { 47 | builtDate = builtDate.replaceAll("-", " "); 48 | buf.append(' ').append(builtDate); 49 | } 50 | versionString = buf.toString(); 51 | return; 52 | } 53 | } catch (IOException e) { 54 | // Ignore this unlikely event 55 | } 56 | } 57 | // We are probably in a IDE 58 | versionString = "Rhino Snapshot"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/LambdaFunction.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | /** 10 | * This class implements a single JavaScript function that has the prototype of the built-in 11 | * Function class, and which is implemented using a single function that can easily be implemented 12 | * using a lambda expression. 13 | */ 14 | public class LambdaFunction extends BaseFunction { 15 | 16 | private static final long serialVersionUID = -8388132362854748293L; 17 | 18 | // The target is expected to be a lambda -- lambdas should not be serialized. 19 | private final transient Callable target; 20 | private final String name; 21 | private final int length; 22 | 23 | /** 24 | * Create a new function. The new object will have the Function prototype and no parent. The 25 | * caller is responsible for binding this object to the appropriate scope. 26 | * 27 | * @param scope scope of the calling context 28 | * @param name name of the function 29 | * @param length the arity of the function 30 | * @param target an object that implements the function in Java. Since Callable is a 31 | * single-function interface this will typically be implemented as a lambda. 32 | */ 33 | public LambdaFunction(Scriptable scope, String name, int length, Callable target) { 34 | this.target = target; 35 | this.name = name; 36 | this.length = length; 37 | ScriptRuntime.setFunctionProtoAndParent(this, scope); 38 | setupDefaultPrototype(); 39 | } 40 | 41 | /** Create a new built-in function, with no name, and no default prototype. */ 42 | public LambdaFunction(Scriptable scope, int length, Callable target) { 43 | this.target = target; 44 | this.length = length; 45 | this.name = ""; 46 | ScriptRuntime.setFunctionProtoAndParent(this, scope); 47 | } 48 | 49 | @Override 50 | public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { 51 | return target.call(cx, scope, thisObj, args); 52 | } 53 | 54 | @Override 55 | public Scriptable construct(Context cx, Scriptable scope, Object[] args) { 56 | throw ScriptRuntime.typeErrorById("msg.no.new", getFunctionName()); 57 | } 58 | 59 | @Override 60 | public int getLength() { 61 | return length; 62 | } 63 | 64 | @Override 65 | public int getArity() { 66 | return length; 67 | } 68 | 69 | @Override 70 | public String getFunctionName() { 71 | return name; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/LambdaSlot.java: -------------------------------------------------------------------------------- 1 | package org.mozilla.javascript; 2 | 3 | import java.util.function.Consumer; 4 | import java.util.function.Supplier; 5 | 6 | /** 7 | * This is a specialization of property access using some lambda functions. It behaves exactly like 8 | * any other slot that has only a value, but instead of getting the value directly, it comes from 9 | * calling the functions. This makes it different from GetterSlot, which lets the user see directly 10 | * that there is a getter or a setter function involved. This makes this class useful for 11 | * implementing properties that behave like any other JavaScript property but which are implemented 12 | * using some native functionality without using reflection. 13 | */ 14 | public class LambdaSlot extends Slot { 15 | private static final long serialVersionUID = -3046681698806493052L; 16 | 17 | LambdaSlot(Slot oldSlot) { 18 | super(oldSlot); 19 | } 20 | 21 | transient Supplier getter; 22 | transient Consumer setter; 23 | 24 | @Override 25 | boolean isValueSlot() { 26 | return false; 27 | } 28 | 29 | @Override 30 | boolean isSetterSlot() { 31 | return false; 32 | } 33 | 34 | @Override 35 | ScriptableObject getPropertyDescriptor(Context cx, Scriptable scope) { 36 | ScriptableObject desc = (ScriptableObject) cx.newObject(scope); 37 | if (getter != null) { 38 | desc.defineProperty("value", getter.get(), ScriptableObject.EMPTY); 39 | } else { 40 | desc.defineProperty("value", value, ScriptableObject.EMPTY); 41 | } 42 | desc.setCommonDescriptorProperties(getAttributes(), true); 43 | return desc; 44 | } 45 | 46 | @Override 47 | public boolean setValue(Object value, Scriptable owner, Scriptable start) { 48 | if (setter != null) { 49 | if (owner == start) { 50 | setter.accept(value); 51 | return true; 52 | } 53 | return false; 54 | } 55 | return super.setValue(value, owner, start); 56 | } 57 | 58 | @Override 59 | public Object getValue(Scriptable start) { 60 | if (getter != null) { 61 | return getter.get(); 62 | } 63 | return super.getValue(start); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/LazyLoadSlot.java: -------------------------------------------------------------------------------- 1 | package org.mozilla.javascript; 2 | 3 | /** 4 | * This is a specialization of Slot to store values that are retrieved via calls to script 5 | * functions. It's used to load built-in objects more efficiently. 6 | */ 7 | public class LazyLoadSlot extends Slot { 8 | LazyLoadSlot(Slot oldSlot) { 9 | super(oldSlot); 10 | } 11 | 12 | @Override 13 | public Object getValue(Scriptable start) { 14 | Object val = this.value; 15 | if (val instanceof LazilyLoadedCtor) { 16 | LazilyLoadedCtor initializer = (LazilyLoadedCtor) val; 17 | try { 18 | initializer.init(); 19 | } finally { 20 | this.value = val = initializer.getValue(); 21 | } 22 | } 23 | return val; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/NativeArrayIterator.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | public final class NativeArrayIterator extends ES6Iterator { 10 | public enum ARRAY_ITERATOR_TYPE { 11 | ENTRIES, 12 | KEYS, 13 | VALUES 14 | } 15 | 16 | private static final long serialVersionUID = 1L; 17 | private static final String ITERATOR_TAG = "ArrayIterator"; 18 | 19 | private ARRAY_ITERATOR_TYPE type; 20 | 21 | static void init(ScriptableObject scope, boolean sealed) { 22 | ES6Iterator.init(scope, sealed, new NativeArrayIterator(), ITERATOR_TAG); 23 | } 24 | 25 | /** 26 | * Only for constructing the prototype object. 27 | */ 28 | private NativeArrayIterator() { 29 | super(); 30 | } 31 | 32 | public NativeArrayIterator(Scriptable scope, Scriptable arrayLike, ARRAY_ITERATOR_TYPE type) { 33 | super(scope, ITERATOR_TAG); 34 | this.index = 0; 35 | this.arrayLike = arrayLike; 36 | this.type = type; 37 | } 38 | 39 | @Override 40 | public String getClassName() { 41 | return "Array Iterator"; 42 | } 43 | 44 | @Override 45 | protected boolean isDone(Context cx, Scriptable scope) { 46 | return index >= NativeArray.getLengthProperty(cx, arrayLike); 47 | } 48 | 49 | @Override 50 | protected Object nextValue(Context cx, Scriptable scope) { 51 | if (type == ARRAY_ITERATOR_TYPE.KEYS) { 52 | return Integer.valueOf(index++); 53 | } 54 | 55 | Object value = arrayLike.get(index, arrayLike); 56 | if (value == Scriptable.NOT_FOUND) { 57 | value = Undefined.instance; 58 | } 59 | 60 | if (type == ARRAY_ITERATOR_TYPE.ENTRIES) { 61 | value = cx.newArray(scope, new Object[] {Integer.valueOf(index), value}); 62 | } 63 | 64 | index++; 65 | return value; 66 | } 67 | 68 | @Override 69 | protected String getTag() { 70 | return ITERATOR_TAG; 71 | } 72 | 73 | private Scriptable arrayLike; 74 | private int index; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/NativeCollectionIterator.java: -------------------------------------------------------------------------------- 1 | package org.mozilla.javascript; 2 | 3 | import java.io.IOException; 4 | import java.io.ObjectInputStream; 5 | import java.io.ObjectOutputStream; 6 | import java.util.Collections; 7 | import java.util.Iterator; 8 | 9 | public class NativeCollectionIterator extends ES6Iterator { 10 | private static final long serialVersionUID = 7094840979404373443L; 11 | private String className; 12 | private Type type; 13 | private transient Iterator iterator = Collections.emptyIterator(); 14 | enum Type { KEYS, VALUES, BOTH } 15 | 16 | static void init(ScriptableObject scope, String tag, boolean sealed) { 17 | ES6Iterator.init(scope, sealed, new NativeCollectionIterator(tag), tag); 18 | } 19 | 20 | public NativeCollectionIterator(String tag) 21 | { 22 | this.className = tag; 23 | this.iterator = Collections.emptyIterator(); 24 | this.type = Type.BOTH; 25 | } 26 | 27 | public NativeCollectionIterator( 28 | Scriptable scope, String className, 29 | Type type, Iterator iterator) 30 | { 31 | super(scope, className); 32 | this.className = className; 33 | this.iterator = iterator; 34 | this.type = type; 35 | } 36 | 37 | @Override 38 | public String getClassName() { 39 | return className; 40 | } 41 | 42 | @Override 43 | protected boolean isDone(Context cx, Scriptable scope) { 44 | return !iterator.hasNext(); 45 | } 46 | 47 | @Override 48 | protected Object nextValue(Context cx, Scriptable scope) { 49 | final Hashtable.Entry e = iterator.next(); 50 | switch (type) { 51 | case KEYS: 52 | return e.key; 53 | case VALUES: 54 | return e.value; 55 | case BOTH: 56 | return cx.newArray(scope, new Object[] { e.key, e.value }); 57 | default: 58 | throw new AssertionError(); 59 | } 60 | } 61 | 62 | private void readObject(ObjectInputStream stream) 63 | throws IOException, ClassNotFoundException 64 | { 65 | stream.defaultReadObject(); 66 | className = (String) stream.readObject(); 67 | type = (Type) stream.readObject(); 68 | iterator = Collections.emptyIterator(); 69 | } 70 | 71 | private void writeObject(ObjectOutputStream stream) 72 | throws IOException 73 | { 74 | stream.defaultWriteObject(); 75 | stream.writeObject(className); 76 | stream.writeObject(type); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/NativeJavaConstructor.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | /** 10 | * This class reflects a single Java constructor into the JavaScript 11 | * environment. It satisfies a request for an overloaded constructor, 12 | * as introduced in LiveConnect 3. 13 | * All NativeJavaConstructors behave as JSRef `bound' methods, in that they 14 | * always construct the same NativeJavaClass regardless of any reparenting 15 | * that may occur. 16 | * 17 | * @author Frank Mitchell 18 | * @see NativeJavaMethod 19 | * @see NativeJavaPackage 20 | * @see NativeJavaClass 21 | */ 22 | 23 | public class NativeJavaConstructor extends BaseFunction 24 | { 25 | private static final long serialVersionUID = -8149253217482668463L; 26 | 27 | MemberBox ctor; 28 | 29 | public NativeJavaConstructor(MemberBox ctor) 30 | { 31 | this.ctor = ctor; 32 | } 33 | 34 | @Override 35 | public Object call(Context cx, Scriptable scope, Scriptable thisObj, 36 | Object[] args) 37 | { 38 | return NativeJavaClass.constructSpecific(cx, scope, args, ctor); 39 | } 40 | 41 | @Override 42 | public String getFunctionName() 43 | { 44 | String sig = JavaMembers.liveConnectSignature(ctor.argTypes); 45 | return "".concat(sig); 46 | } 47 | 48 | @Override 49 | public String toString() 50 | { 51 | return "[JavaConstructor " + ctor.getName() + "]"; 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/NativeStringIterator.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | public final class NativeStringIterator extends ES6Iterator { 10 | private static final long serialVersionUID = 1L; 11 | private static final String ITERATOR_TAG = "StringIterator"; 12 | 13 | static void init(ScriptableObject scope, boolean sealed) { 14 | ES6Iterator.init(scope, sealed, new NativeStringIterator(), ITERATOR_TAG); 15 | } 16 | 17 | /** 18 | * Only for constructing the prototype object. 19 | */ 20 | private NativeStringIterator() { 21 | super(); 22 | } 23 | 24 | NativeStringIterator(Scriptable scope, Object stringLike) { 25 | super(scope, ITERATOR_TAG); 26 | this.index = 0; 27 | this.string = ScriptRuntime.toString(stringLike); 28 | } 29 | 30 | @Override 31 | public String getClassName() { 32 | return "String Iterator"; 33 | } 34 | 35 | @Override 36 | protected boolean isDone(Context cx, Scriptable scope) { 37 | return index >= string.length(); 38 | } 39 | 40 | @Override 41 | protected Object nextValue(Context cx, Scriptable scope) { 42 | int newIndex = string.offsetByCodePoints(index, 1); 43 | Object value = string.substring(index, newIndex); 44 | index = newIndex; 45 | return value; 46 | } 47 | 48 | @Override 49 | protected String getTag() { 50 | return ITERATOR_TAG; 51 | } 52 | 53 | private String string; 54 | private int index; 55 | } 56 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/Ref.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | import java.io.Serializable; 10 | 11 | /** 12 | * Generic notion of reference object that know how to query/modify the 13 | * target objects based on some property/index. 14 | */ 15 | public abstract class Ref implements Serializable 16 | { 17 | 18 | private static final long serialVersionUID = 4044540354730911424L; 19 | 20 | public boolean has(Context cx) 21 | { 22 | return true; 23 | } 24 | 25 | public abstract Object get(Context cx); 26 | 27 | /** 28 | * @deprecated Use {@link #set(Context, Scriptable, Object)} instead 29 | */ 30 | @Deprecated 31 | public abstract Object set(Context cx, Object value); 32 | 33 | public Object set(Context cx, Scriptable scope, Object value) 34 | { 35 | return set(cx, value); 36 | } 37 | 38 | public boolean delete(Context cx) 39 | { 40 | return false; 41 | } 42 | 43 | } 44 | 45 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/RefCallable.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | /** 10 | * Object that can allows assignments to the result of function calls. 11 | */ 12 | public interface RefCallable extends Callable 13 | { 14 | /** 15 | * Perform function call in reference context. 16 | * The args array reference should not be stored in any object that is 17 | * can be GC-reachable after this method returns. If this is necessary, 18 | * for example, to implement {@link Ref} methods, then store args.clone(), 19 | * not args array itself. 20 | * 21 | * @param cx the current Context for this thread 22 | * @param thisObj the JavaScript this object 23 | * @param args the array of arguments 24 | */ 25 | public Ref refCall(Context cx, Scriptable thisObj, Object[] args); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/RegExpProxy.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | /** 10 | * A proxy for the regexp package, so that the regexp package can be 11 | * loaded optionally. 12 | * 13 | * @author Norris Boyd 14 | */ 15 | public interface RegExpProxy 16 | { 17 | // Types of regexp actions 18 | public static final int RA_MATCH = 1; 19 | public static final int RA_REPLACE = 2; 20 | public static final int RA_SEARCH = 3; 21 | 22 | public boolean isRegExp(Scriptable obj); 23 | 24 | public Object compileRegExp(Context cx, String source, String flags); 25 | 26 | public Scriptable wrapRegExp(Context cx, Scriptable scope, 27 | Object compiled); 28 | 29 | public Object action(Context cx, Scriptable scope, 30 | Scriptable thisObj, Object[] args, 31 | int actionType); 32 | 33 | public int find_split(Context cx, Scriptable scope, String target, 34 | String separator, Scriptable re, 35 | int[] ip, int[] matchlen, 36 | boolean[] matched, String[][] parensp); 37 | 38 | public Object js_split(Context _cx, Scriptable _scope, 39 | String thisString, Object[] _args); 40 | } 41 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/RhinoSecurityManager.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript; 6 | 7 | /** 8 | * A java.lang.SecurityManager subclass that provides access to 9 | * the current top-most script class on the execution stack. This can be used 10 | * to get the class loader or protection domain of the script that triggered 11 | * the current action. It is required for JavaAdapters to have the same 12 | * ProtectionDomain as the script code that created them. 13 | * Embeddings that implement their own SecurityManager can use this as base class. 14 | */ 15 | public class RhinoSecurityManager extends SecurityManager { 16 | 17 | /** 18 | * Get the class of the top-most stack element representing a script. 19 | * @return The class of the top-most script in the current stack, 20 | * or null if no script is currently running 21 | */ 22 | protected Class getCurrentScriptClass() { 23 | Class[] context = getClassContext(); 24 | for (Class c : context) { 25 | if (c != InterpretedFunction.class && NativeFunction.class.isAssignableFrom(c) || 26 | PolicySecurityController.SecureCaller.class.isAssignableFrom(c)) { 27 | return c; 28 | } 29 | } 30 | return null; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/Script.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript; 10 | 11 | /** 12 | * All compiled scripts implement this interface. 13 | *

14 | * This class encapsulates script execution relative to an 15 | * object scope. 16 | * @since 1.3 17 | * @author Norris Boyd 18 | */ 19 | 20 | public interface Script { 21 | 22 | /** 23 | * Execute the script. 24 | *

25 | * The script is executed in a particular runtime Context, which 26 | * must be associated with the current thread. 27 | * The script is executed relative to a scope--definitions and 28 | * uses of global top-level variables and functions will access 29 | * properties of the scope object. For compliant ECMA 30 | * programs, the scope must be an object that has been initialized 31 | * as a global object using Context.initStandardObjects. 32 | *

33 | * 34 | * @param cx the Context associated with the current thread 35 | * @param scope the scope to execute relative to 36 | * @return the result of executing the script 37 | * @see org.mozilla.javascript.Context#initStandardObjects() 38 | */ 39 | public Object exec(Context cx, Scriptable scope); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ScriptRuntimeES6.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | public class ScriptRuntimeES6 { 10 | 11 | public static Object requireObjectCoercible(Context cx, Object val, IdFunctionObject idFuncObj) { 12 | if (val == null || Undefined.isUndefined(val)) { 13 | throw ScriptRuntime.typeErrorById("msg.called.null.or.undefined", idFuncObj.getTag(), idFuncObj.getFunctionName()); 14 | } 15 | return val; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/SecurityUtilities.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | import java.security.AccessController; 10 | import java.security.PrivilegedAction; 11 | import java.security.ProtectionDomain; 12 | 13 | /** 14 | * @author Attila Szegedi 15 | */ 16 | public class SecurityUtilities 17 | { 18 | /** 19 | * Retrieves a system property within a privileged block. Use it only when 20 | * the property is used from within Rhino code and is not passed out of it. 21 | * @param name the name of the system property 22 | * @return the value of the system property 23 | */ 24 | public static String getSystemProperty(final String name) 25 | { 26 | return AccessController.doPrivileged( 27 | new PrivilegedAction() 28 | { 29 | @Override 30 | public String run() 31 | { 32 | return System.getProperty(name); 33 | } 34 | }); 35 | } 36 | 37 | public static ProtectionDomain getProtectionDomain(final Class clazz) 38 | { 39 | return AccessController.doPrivileged( 40 | new PrivilegedAction() 41 | { 42 | @Override 43 | public ProtectionDomain run() 44 | { 45 | return clazz.getProtectionDomain(); 46 | } 47 | }); 48 | } 49 | 50 | /** 51 | * Look up the top-most element in the current stack representing a 52 | * script and return its protection domain. This relies on the system-wide 53 | * SecurityManager being an instance of {@link RhinoSecurityManager}, 54 | * otherwise it returns null. 55 | * @return The protection of the top-most script in the current stack, or null 56 | */ 57 | public static ProtectionDomain getScriptProtectionDomain() { 58 | final SecurityManager securityManager = System.getSecurityManager(); 59 | if (securityManager instanceof RhinoSecurityManager) { 60 | return AccessController.doPrivileged( 61 | new PrivilegedAction() { 62 | @Override 63 | public ProtectionDomain run() { 64 | Class c = ((RhinoSecurityManager) securityManager) 65 | .getCurrentScriptClass(); 66 | return c == null ? null : c.getProtectionDomain(); 67 | } 68 | } 69 | ); 70 | } 71 | return null; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/SlotMap.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | /** 10 | * A SlotMap is an interface to the main data structure that contains all the "Slots" that back a 11 | * ScriptableObject. It is the primary property map in Rhino. It is Iterable but does not implement 12 | * java.util.Map because that comes with a bunch of overhead that we do not need. 13 | * 14 | *

This class generally has a bit of a strange interface, and its interactions with 15 | * ScriptableObject are complex. Many attempts to make this interface more elegant have resulted in 16 | * substantial performance regressions so we are doing the best that we can. 17 | */ 18 | public interface SlotMap extends Iterable { 19 | 20 | /** Return the size of the map. */ 21 | int size(); 22 | 23 | /** Return whether the map is empty. */ 24 | boolean isEmpty(); 25 | 26 | /** 27 | * Return the Slot that matches EITHER "key" or "index". (It will use "key" if it is not null, 28 | * and otherwise "index".) If no slot exists, then create a default slot class. 29 | * 30 | * @param key The key for the slot, which should be a String or a Symbol. 31 | * @param index if key is zero, then this will be used as the key instead. 32 | * @param attributes the attributes to be set on the slot if a new slot is created. Existing 33 | * slots will not be modified. 34 | * @return a Slot, which will be created anew if no such slot exists. 35 | */ 36 | Slot modify(Object key, int index, int attributes); 37 | 38 | /** 39 | * Retrieve the slot at EITHER key or index, or return null if the slot cannot be found. 40 | * 41 | * @param key The key for the slot, which should be a String or a Symbol. 42 | * @param index if key is zero, then this will be used as the key instead. 43 | * @return either the Slot that matched the key and index, or null 44 | */ 45 | Slot query(Object key, int index); 46 | 47 | /** Replace "slot" with a new slot. This is used to change slot types. */ 48 | void replace(Slot oldSlot, Slot newSlot); 49 | 50 | /** 51 | * Insert a new slot to the map. Both "name" and "indexOrHash" must be populated. Note that 52 | * ScriptableObject generally adds slots via the "modify" method. 53 | */ 54 | void add(Slot newSlot); 55 | 56 | /** 57 | * Remove the slot at either "key" or "index". 58 | * 59 | * @param key The key for the slot, which should be a String or a Symbol. 60 | * @param index if key is zero, then this will be used as the key instead. 61 | */ 62 | void remove(Object key, int index); 63 | } 64 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/StackStyle.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript; 6 | 7 | /** This class distinguishes between the three different supported stack trace formats. */ 8 | public enum StackStyle { 9 | /** 10 | * This is the default stack trace style in Rhino, which is like Java: 11 | * at fileName:lineNumber (functionName) 12 | */ 13 | RHINO, 14 | 15 | /** 16 | * This stack trace style comes from the old Mozilla code: 17 | * functionName()@fileName:lineNumber 18 | */ 19 | MOZILLA, 20 | 21 | /** 22 | * This is the same as MOZILLA but uses LF as speparator instead of the system dependent line 23 | * separator. 24 | */ 25 | MOZILLA_LF, 26 | 27 | /** 28 | * This stack trace style matches that output from V8, either: 29 | * at functionName (fileName:lineNumber:columnNumber) or, for anonymous functions: 30 | * at fileName:lineNumber:columnNumber 31 | */ 32 | V8 33 | } 34 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/Symbol.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | /** 10 | * A Symbol is a JavaScript objecy that obeys the special properties of the 11 | * Symbol prototype. This interface lets us possibly support multiple 12 | * implementations of Symbol. 13 | * 14 | * @since 1.7.8 15 | */ 16 | 17 | public interface Symbol 18 | { 19 | } 20 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/SymbolKey.java: -------------------------------------------------------------------------------- 1 | package org.mozilla.javascript; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * A SymbolKey is one of the implementations of Symbol. It is really there 7 | * so that we can easily use pre-defined symbols as keys in native code. 8 | * A SymbolKey has the special property that two NativeSymbol objects 9 | * with the same key are equal. 10 | */ 11 | 12 | public class SymbolKey 13 | implements Symbol, Serializable 14 | { 15 | private static final long serialVersionUID = -6019782713330994754L; 16 | 17 | // These are common SymbolKeys that are equivalent to well-known symbols 18 | // defined in ECMAScript. 19 | public static final SymbolKey ITERATOR = new SymbolKey("Symbol.iterator"); 20 | public static final SymbolKey TO_STRING_TAG = new SymbolKey("Symbol.toStringTag"); 21 | public static final SymbolKey SPECIES = new SymbolKey("Symbol.species"); 22 | public static final SymbolKey HAS_INSTANCE = new SymbolKey("Symbol.hasInstance"); 23 | public static final SymbolKey IS_CONCAT_SPREADABLE = new SymbolKey("Symbol.isConcatSpreadable"); 24 | public static final SymbolKey IS_REGEXP = new SymbolKey("Symbol.isRegExp"); 25 | public static final SymbolKey TO_PRIMITIVE = new SymbolKey("Symbol.toPrimitive"); 26 | public static final SymbolKey MATCH = new SymbolKey("Symbol.match"); 27 | public static final SymbolKey REPLACE = new SymbolKey("Symbol.replace"); 28 | public static final SymbolKey SEARCH = new SymbolKey("Symbol.search"); 29 | public static final SymbolKey SPLIT = new SymbolKey("Symbol.split"); 30 | public static final SymbolKey UNSCOPABLES = new SymbolKey("Symbol.unscopables"); 31 | 32 | private String name; 33 | 34 | public SymbolKey(String name) 35 | { 36 | this.name = name; 37 | } 38 | 39 | public String getName() { 40 | return name; 41 | } 42 | 43 | @Override 44 | public int hashCode() 45 | { 46 | return System.identityHashCode(this); 47 | } 48 | 49 | @Override 50 | public boolean equals(Object o) 51 | { 52 | if (o instanceof SymbolKey) { 53 | return o == this; 54 | } 55 | if (o instanceof NativeSymbol) { 56 | return ((NativeSymbol) o).getKey() == this; 57 | } 58 | return false; 59 | } 60 | 61 | @Override 62 | public String toString() 63 | { 64 | if (name == null) { 65 | return "Symbol()"; 66 | } 67 | return "Symbol(" + name + ')'; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/SymbolScriptable.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | /** 10 | * This interface may be combined with any object that implements Scriptable 11 | * to add support for properties keyed by Symbol objects (as opposed to 12 | * String and number objects as in previous versions of JavaScript. 13 | * It's separated into its own interface so that the addition of Symbol 14 | * support does not break compatibility for existing code. 15 | * 16 | * @since 1.7.8 17 | */ 18 | 19 | public interface SymbolScriptable 20 | { 21 | /** 22 | * Return the value of the property with the specified key, or 23 | * NOT_FOUND. 24 | */ 25 | Object get(Symbol key, Scriptable start); 26 | 27 | /** 28 | * Return true if the specified property exists. 29 | */ 30 | boolean has(Symbol key, Scriptable start); 31 | 32 | /** 33 | * Add a new property to to the object. 34 | */ 35 | void put(Symbol key, Scriptable start, Object value); 36 | 37 | /** 38 | * Delete a property with the specified key. 39 | */ 40 | void delete(Symbol key); 41 | } 42 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/Synchronizer.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript; 10 | 11 | /** 12 | * This class provides support for implementing Java-style synchronized 13 | * methods in Javascript. 14 | * 15 | * Synchronized functions are created from ordinary Javascript 16 | * functions by the Synchronizer constructor, e.g. 17 | * new Packages.org.mozilla.javascript.Synchronizer(fun). 18 | * The resulting object is a function that establishes an exclusive 19 | * lock on the this object of its invocation. 20 | * 21 | * The Rhino shell provides a short-cut for the creation of 22 | * synchronized methods: sync(fun) has the same effect as 23 | * calling the above constructor. 24 | * 25 | * @see org.mozilla.javascript.Delegator 26 | * @author Matthias Radestock 27 | */ 28 | 29 | public class Synchronizer extends Delegator { 30 | 31 | private Object syncObject; 32 | 33 | /** 34 | * Create a new synchronized function from an existing one. 35 | * 36 | * @param obj the existing function 37 | */ 38 | public Synchronizer(Scriptable obj) { 39 | super(obj); 40 | } 41 | 42 | /** 43 | * Create a new synchronized function from an existing one using 44 | * an explicit object as synchronization object. 45 | * 46 | * @param obj the existing function 47 | * @param syncObject the object to synchronized on 48 | */ 49 | public Synchronizer(Scriptable obj, Object syncObject) { 50 | super(obj); 51 | this.syncObject = syncObject; 52 | } 53 | 54 | /** 55 | * @see org.mozilla.javascript.Function#call 56 | */ 57 | @Override 58 | public Object call(Context cx, Scriptable scope, Scriptable thisObj, 59 | Object[] args) 60 | { 61 | Object sync = syncObject != null ? syncObject : thisObj; 62 | synchronized(sync instanceof Wrapper ? ((Wrapper)sync).unwrap() : sync) { 63 | return ((Function)obj).call(cx,scope,thisObj,args); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/UniqueTag.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | import java.io.Serializable; 10 | 11 | /** 12 | * Class instances represent serializable tags to mark special Object values. 13 | *

14 | * Compatibility note: under jdk 1.1 use 15 | * org.mozilla.javascript.serialize.ScriptableInputStream to read serialized 16 | * instances of UniqueTag as under this JDK version the default 17 | * ObjectInputStream would not restore them correctly as it lacks support 18 | * for readResolve method 19 | */ 20 | public final class UniqueTag implements Serializable 21 | { 22 | private static final long serialVersionUID = -4320556826714577259L; 23 | 24 | private static final int ID_NOT_FOUND = 1; 25 | private static final int ID_NULL_VALUE = 2; 26 | private static final int ID_DOUBLE_MARK = 3; 27 | 28 | /** 29 | * Tag to mark non-existing values. 30 | */ 31 | public static final UniqueTag 32 | NOT_FOUND = new UniqueTag(ID_NOT_FOUND); 33 | 34 | /** 35 | * Tag to distinguish between uninitialized and null values. 36 | */ 37 | public static final UniqueTag 38 | NULL_VALUE = new UniqueTag(ID_NULL_VALUE); 39 | 40 | /** 41 | * Tag to indicate that a object represents "double" with the real value 42 | * stored somewhere else. 43 | */ 44 | public static final UniqueTag 45 | DOUBLE_MARK = new UniqueTag(ID_DOUBLE_MARK); 46 | 47 | private final int tagId; 48 | 49 | private UniqueTag(int tagId) 50 | { 51 | this.tagId = tagId; 52 | } 53 | 54 | public Object readResolve() 55 | { 56 | switch (tagId) { 57 | case ID_NOT_FOUND: 58 | return NOT_FOUND; 59 | case ID_NULL_VALUE: 60 | return NULL_VALUE; 61 | case ID_DOUBLE_MARK: 62 | return DOUBLE_MARK; 63 | } 64 | throw new IllegalStateException(String.valueOf(tagId)); 65 | } 66 | 67 | // Overridden for better debug printouts 68 | @Override 69 | public String toString() 70 | { 71 | String name; 72 | switch (tagId) { 73 | case ID_NOT_FOUND: 74 | name = "NOT_FOUND"; 75 | break; 76 | case ID_NULL_VALUE: 77 | name = "NULL_VALUE"; 78 | break; 79 | case ID_DOUBLE_MARK: 80 | name = "DOUBLE_MARK"; 81 | break; 82 | default: 83 | throw Kit.codeBug(); 84 | } 85 | return super.toString()+": "+name; 86 | } 87 | 88 | } 89 | 90 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/WrappedException.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript; 8 | 9 | /** 10 | * A wrapper for runtime exceptions. 11 | * 12 | * Used by the JavaScript runtime to wrap and propagate exceptions that occur 13 | * during runtime. 14 | * 15 | * @author Norris Boyd 16 | */ 17 | public class WrappedException extends EvaluatorException 18 | { 19 | private static final long serialVersionUID = -1551979216966520648L; 20 | 21 | /** 22 | * @see Context#throwAsScriptRuntimeEx(Throwable e) 23 | */ 24 | public WrappedException(Throwable exception) 25 | { 26 | super("Wrapped " + exception); 27 | this.exception = exception; 28 | this.initCause(exception); 29 | 30 | int[] linep = { 0 }; 31 | String sourceName = Context.getSourcePositionFromStack(linep); 32 | int lineNumber = linep[0]; 33 | if (sourceName != null) { 34 | initSourceName(sourceName); 35 | } 36 | if (lineNumber != 0) { 37 | initLineNumber(lineNumber); 38 | } 39 | } 40 | 41 | /** 42 | * Get the wrapped exception. 43 | * 44 | * @return the exception that was presented as a argument to the 45 | * constructor when this object was created 46 | */ 47 | public Throwable getWrappedException() 48 | { 49 | return exception; 50 | } 51 | 52 | /** 53 | * @deprecated Use {@link #getWrappedException()} instead. 54 | */ 55 | @Deprecated 56 | public Object unwrap() 57 | { 58 | return getWrappedException(); 59 | } 60 | 61 | private Throwable exception; 62 | } 63 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/Wrapper.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript; 10 | 11 | /** 12 | * Objects that can wrap other values for reflection in the JS environment 13 | * will implement Wrapper. 14 | * 15 | * Wrapper defines a single method that can be called to unwrap the object. 16 | */ 17 | 18 | public interface Wrapper { 19 | 20 | /** 21 | * Unwrap the object by returning the wrapped value. 22 | * 23 | * @return a wrapped value 24 | */ 25 | public Object unwrap(); 26 | } 27 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/annotations/JSConstructor.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.annotations; 6 | 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.ElementType; 9 | import java.lang.annotation.Retention; 10 | import java.lang.annotation.RetentionPolicy; 11 | import java.lang.annotation.Target; 12 | 13 | /** 14 | * An annotation that marks a Java method as JavaScript constructor. This can 15 | * be used as an alternative to the jsConstructor naming convention desribed in 16 | * {@link org.mozilla.javascript.ScriptableObject#defineClass(org.mozilla.javascript.Scriptable, java.lang.Class)}. 17 | */ 18 | @Documented 19 | @Retention(RetentionPolicy.RUNTIME) 20 | @Target({ElementType.CONSTRUCTOR, ElementType.METHOD}) 21 | public @interface JSConstructor { 22 | } 23 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/annotations/JSFunction.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.annotations; 6 | 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.ElementType; 9 | import java.lang.annotation.Retention; 10 | import java.lang.annotation.RetentionPolicy; 11 | import java.lang.annotation.Target; 12 | 13 | /** 14 | * An annotation that marks a Java method as JavaScript function. This can 15 | * be used as an alternative to the jsFunction_ prefix desribed in 16 | * {@link org.mozilla.javascript.ScriptableObject#defineClass(org.mozilla.javascript.Scriptable, java.lang.Class)}. 17 | */ 18 | @Documented 19 | @Retention(RetentionPolicy.RUNTIME) 20 | @Target(ElementType.METHOD) 21 | public @interface JSFunction { 22 | String value() default ""; 23 | } 24 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/annotations/JSGetter.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.annotations; 6 | 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.ElementType; 9 | import java.lang.annotation.Retention; 10 | import java.lang.annotation.RetentionPolicy; 11 | import java.lang.annotation.Target; 12 | 13 | /** 14 | * An annotation that marks a Java method as JavaScript getter. This can 15 | * be used as an alternative to the jsGet_ prefix desribed in 16 | * {@link org.mozilla.javascript.ScriptableObject#defineClass(org.mozilla.javascript.Scriptable, java.lang.Class)}. 17 | */ 18 | @Documented 19 | @Retention(RetentionPolicy.RUNTIME) 20 | @Target(ElementType.METHOD) 21 | public @interface JSGetter { 22 | String value() default ""; 23 | } 24 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/annotations/JSSetter.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.annotations; 6 | 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.ElementType; 9 | import java.lang.annotation.Retention; 10 | import java.lang.annotation.RetentionPolicy; 11 | import java.lang.annotation.Target; 12 | 13 | /** 14 | * An annotation that marks a Java method as JavaScript setter. This can 15 | * be used as an alternative to the jsSet_ prefix desribed in 16 | * {@link org.mozilla.javascript.ScriptableObject#defineClass(org.mozilla.javascript.Scriptable, java.lang.Class)}. 17 | */ 18 | @Documented 19 | @Retention(RetentionPolicy.RUNTIME) 20 | @Target(ElementType.METHOD) 21 | public @interface JSSetter { 22 | String value() default ""; 23 | } 24 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/annotations/JSStaticFunction.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.annotations; 6 | 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.ElementType; 9 | import java.lang.annotation.Retention; 10 | import java.lang.annotation.RetentionPolicy; 11 | import java.lang.annotation.Target; 12 | 13 | /** 14 | * An annotation that marks a Java method as JavaScript static function. This can 15 | * be used as an alternative to the jsStaticFunction_ prefix desribed in 16 | * {@link org.mozilla.javascript.ScriptableObject#defineClass(org.mozilla.javascript.Scriptable, java.lang.Class)}. 17 | */ 18 | @Documented 19 | @Retention(RetentionPolicy.RUNTIME) 20 | @Target(ElementType.METHOD) 21 | public @interface JSStaticFunction { 22 | String value() default ""; 23 | } 24 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/ArrayComprehensionLoop.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * AST node for a single 'for (foo in bar)' loop construct in a JavaScript 1.7 13 | * Array comprehension. This node type is almost equivalent to a 14 | * {@link ForInLoop}, except that it has no body statement. 15 | * Node type is {@link Token#FOR}. 16 | */ 17 | public class ArrayComprehensionLoop extends ForInLoop { 18 | 19 | public ArrayComprehensionLoop() { 20 | } 21 | 22 | public ArrayComprehensionLoop(int pos) { 23 | super(pos); 24 | } 25 | 26 | public ArrayComprehensionLoop(int pos, int len) { 27 | super(pos, len); 28 | } 29 | 30 | /** 31 | * Returns {@code null} for loop body 32 | * @return loop body (always {@code null} for this node type) 33 | */ 34 | @Override 35 | public AstNode getBody() { 36 | return null; 37 | } 38 | 39 | /** 40 | * Throws an exception on attempts to set the loop body. 41 | * @param body loop body 42 | * @throws UnsupportedOperationException 43 | */ 44 | @Override 45 | public void setBody(AstNode body) { 46 | throw new UnsupportedOperationException("this node type has no body"); 47 | } 48 | 49 | @Override 50 | public String toSource(int depth) { 51 | return makeIndent(depth) 52 | + " for " 53 | + (isForEach()?"each ":"") 54 | + "(" 55 | + iterator.toSource(0) 56 | + (isForOf()?" of ":" in ") 57 | + iteratedObject.toSource(0) 58 | + ")"; 59 | } 60 | 61 | /** 62 | * Visits the iterator expression and the iterated object expression. 63 | * There is no body-expression for this loop type. 64 | */ 65 | @Override 66 | public void visit(NodeVisitor v) { 67 | if (v.visit(this)) { 68 | iterator.visit(v); 69 | iteratedObject.visit(v); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/Assignment.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | /** 10 | * AST node representing the set of assignment operators such as {@code =}, 11 | * {@code *=} and {@code +=}. 12 | */ 13 | public class Assignment extends InfixExpression { 14 | 15 | public Assignment() { 16 | } 17 | 18 | public Assignment(int pos) { 19 | super(pos); 20 | } 21 | 22 | public Assignment(int pos, int len) { 23 | super(pos, len); 24 | } 25 | 26 | public Assignment(int pos, int len, AstNode left, AstNode right) { 27 | super(pos, len, left, right); 28 | } 29 | 30 | public Assignment(AstNode left, AstNode right) { 31 | super(left, right); 32 | } 33 | 34 | public Assignment(int operator, AstNode left, 35 | AstNode right, int operatorPos) { 36 | super(operator, left, right, operatorPos); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/BigIntLiteral.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import java.math.BigInteger; 10 | import org.mozilla.javascript.Token; 11 | 12 | /** AST node for a BigInt literal. Node type is {@link Token#BIGINT}. */ 13 | public class BigIntLiteral extends AstNode { 14 | 15 | private String value; 16 | private BigInteger bigInt; 17 | 18 | { 19 | type = Token.BIGINT; 20 | } 21 | 22 | public BigIntLiteral() {} 23 | 24 | public BigIntLiteral(int pos) { 25 | super(pos); 26 | } 27 | 28 | public BigIntLiteral(int pos, int len) { 29 | super(pos, len); 30 | } 31 | 32 | /** Constructor. Sets the length to the length of the {@code value} string. */ 33 | public BigIntLiteral(int pos, String value) { 34 | super(pos); 35 | setValue(value); 36 | setLength(value.length()); 37 | } 38 | 39 | /** Constructor. Sets the length to the length of the {@code value} string. */ 40 | public BigIntLiteral(int pos, String value, BigInteger bigInt) { 41 | this(pos, value); 42 | setBigInt(bigInt); 43 | } 44 | 45 | /** Returns the node's string value (the original source token) */ 46 | public String getValue() { 47 | return value; 48 | } 49 | 50 | /** 51 | * Sets the node's value 52 | * 53 | * @throws IllegalArgumentException} if value is {@code null} 54 | */ 55 | public void setValue(String value) { 56 | assertNotNull(value); 57 | this.value = value; 58 | } 59 | 60 | /** Gets the {@code BigInteger} value. */ 61 | @Override 62 | public BigInteger getBigInt() { 63 | return bigInt; 64 | } 65 | 66 | /** Sets the node's {@code BigInteger} value. */ 67 | @Override 68 | public void setBigInt(BigInteger value) { 69 | bigInt = value; 70 | } 71 | 72 | @Override 73 | public String toSource(int depth) { 74 | return makeIndent(depth) + (bigInt == null ? "" : bigInt.toString()); 75 | } 76 | 77 | /** Visits this node. There are no children to visit. */ 78 | @Override 79 | public void visit(NodeVisitor v) { 80 | v.visit(this); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/Block.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Node; 10 | import org.mozilla.javascript.Token; 11 | 12 | /** 13 | * A block statement delimited by curly braces. The node position is the 14 | * position of the open-curly, and the length extends to the position of 15 | * the close-curly. Node type is {@link Token#BLOCK}. 16 | * 17 | *

Block :
18 |  *     { Statement* }
19 | */ 20 | public class Block extends AstNode { 21 | 22 | { 23 | this.type = Token.BLOCK; 24 | } 25 | 26 | public Block() { 27 | } 28 | 29 | public Block(int pos) { 30 | super(pos); 31 | } 32 | 33 | public Block(int pos, int len) { 34 | super(pos, len); 35 | } 36 | 37 | /** 38 | * Alias for {@link #addChild}. 39 | */ 40 | public void addStatement(AstNode statement) { 41 | addChild(statement); 42 | } 43 | 44 | @Override 45 | public String toSource(int depth) { 46 | StringBuilder sb = new StringBuilder(); 47 | sb.append(makeIndent(depth)); 48 | sb.append("{\n"); 49 | for (Node kid : this) { 50 | AstNode astNodeKid = (AstNode)kid; 51 | sb.append(astNodeKid.toSource(depth+1)); 52 | if(astNodeKid.getType() == Token.COMMENT) { 53 | sb.append("\n"); 54 | } 55 | } 56 | sb.append(makeIndent(depth)); 57 | sb.append("}"); 58 | if(this.getInlineComment() != null) { 59 | sb.append(this.getInlineComment().toSource(depth)); 60 | } 61 | sb.append("\n"); 62 | return sb.toString(); 63 | } 64 | 65 | @Override 66 | public void visit(NodeVisitor v) { 67 | if (v.visit(this)) { 68 | for (Node kid : this) { 69 | ((AstNode)kid).visit(v); 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/DestructuringForm.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | /** 10 | * Common interface for {@link ArrayLiteral} and {@link ObjectLiteral} 11 | * node types, both of which may appear in "destructuring" expressions or 12 | * contexts. 13 | */ 14 | public interface DestructuringForm { 15 | 16 | /** 17 | * Marks this node as being a destructuring form - that is, appearing 18 | * in a context such as {@code for ([a, b] in ...)} where it's the 19 | * target of a destructuring assignment. 20 | */ 21 | void setIsDestructuring(boolean destructuring); 22 | 23 | /** 24 | * Returns true if this node is in a destructuring position: 25 | * a function parameter, the target of a variable initializer, the 26 | * iterator of a for..in loop, etc. 27 | */ 28 | boolean isDestructuring(); 29 | } 30 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/DoLoop.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * Do statement. Node type is {@link Token#DO}. 13 | * 14 | *
DoLoop:
15 |  * do Statement while ( Expression ) ;
16 | */ 17 | public class DoLoop extends Loop { 18 | 19 | private AstNode condition; 20 | private int whilePosition = -1; 21 | 22 | { 23 | type = Token.DO; 24 | } 25 | 26 | public DoLoop() { 27 | } 28 | 29 | public DoLoop(int pos) { 30 | super(pos); 31 | } 32 | 33 | public DoLoop(int pos, int len) { 34 | super(pos, len); 35 | } 36 | 37 | /** 38 | * Returns loop condition 39 | */ 40 | public AstNode getCondition() { 41 | return condition; 42 | } 43 | 44 | /** 45 | * Sets loop condition, and sets its parent to this node. 46 | * @throws IllegalArgumentException if condition is null 47 | */ 48 | public void setCondition(AstNode condition) { 49 | assertNotNull(condition); 50 | this.condition = condition; 51 | condition.setParent(this); 52 | } 53 | 54 | /** 55 | * Returns source position of "while" keyword 56 | */ 57 | public int getWhilePosition() { 58 | return whilePosition; 59 | } 60 | 61 | /** 62 | * Sets source position of "while" keyword 63 | */ 64 | public void setWhilePosition(int whilePosition) { 65 | this.whilePosition = whilePosition; 66 | } 67 | 68 | @Override 69 | public String toSource(int depth) { 70 | StringBuilder sb = new StringBuilder(); 71 | sb.append(makeIndent(depth)); 72 | sb.append("do "); 73 | if(this.getInlineComment() != null) { 74 | sb.append(this.getInlineComment().toSource(depth + 1)).append("\n"); 75 | } 76 | sb.append(body.toSource(depth).trim()); 77 | sb.append(" while ("); 78 | sb.append(condition.toSource(0)); 79 | sb.append(");\n"); 80 | return sb.toString(); 81 | } 82 | 83 | /** 84 | * Visits this node, the body, and then the while-expression. 85 | */ 86 | @Override 87 | public void visit(NodeVisitor v) { 88 | if (v.visit(this)) { 89 | body.visit(v); 90 | condition.visit(v); 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/EmptyExpression.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * AST node for an empty expression. Node type is {@link Token#EMPTY}.

13 | * 14 | * To create an empty statement, wrap it with an {@link ExpressionStatement}. 15 | */ 16 | public class EmptyExpression extends AstNode { 17 | 18 | { 19 | type = Token.EMPTY; 20 | } 21 | 22 | public EmptyExpression() { 23 | } 24 | 25 | public EmptyExpression(int pos) { 26 | super(pos); 27 | } 28 | 29 | public EmptyExpression(int pos, int len) { 30 | super(pos, len); 31 | } 32 | 33 | @Override 34 | public String toSource(int depth) { 35 | return makeIndent(depth); 36 | } 37 | 38 | /** 39 | * Visits this node. There are no children. 40 | */ 41 | @Override 42 | public void visit(NodeVisitor v) { 43 | v.visit(this); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/EmptyStatement.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * AST node for an empty statement. Node type is {@link Token#EMPTY}. 13 | * 14 | */ 15 | public class EmptyStatement extends AstNode { 16 | 17 | { 18 | type = Token.EMPTY; 19 | } 20 | 21 | public EmptyStatement() { 22 | } 23 | 24 | public EmptyStatement(int pos) { 25 | super(pos); 26 | } 27 | 28 | public EmptyStatement(int pos, int len) { 29 | super(pos, len); 30 | } 31 | 32 | @Override 33 | public String toSource(int depth) { 34 | StringBuilder sb = new StringBuilder(); 35 | sb.append(makeIndent(depth)).append(";\n"); 36 | return sb.toString(); 37 | } 38 | 39 | /** 40 | * Visits this node. There are no children. 41 | */ 42 | @Override 43 | public void visit(NodeVisitor v) { 44 | v.visit(this); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/ErrorNode.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * AST node representing a parse error or a warning. Node type is 13 | * {@link Token#ERROR}. 14 | */ 15 | public class ErrorNode extends AstNode { 16 | 17 | private String message; 18 | 19 | { 20 | type = Token.ERROR; 21 | } 22 | 23 | public ErrorNode() { 24 | } 25 | 26 | public ErrorNode(int pos) { 27 | super(pos); 28 | } 29 | 30 | public ErrorNode(int pos, int len) { 31 | super(pos, len); 32 | } 33 | 34 | /** 35 | * Returns error message key 36 | */ 37 | public String getMessage() { 38 | return message; 39 | } 40 | 41 | /** 42 | * Sets error message key 43 | */ 44 | public void setMessage(String message) { 45 | this.message = message; 46 | } 47 | 48 | @Override 49 | public String toSource(int depth) { 50 | return ""; 51 | } 52 | 53 | /** 54 | * Error nodes are not visited during normal visitor traversals, 55 | * but comply with the {@link AstNode#visit} interface. 56 | */ 57 | @Override 58 | public void visit(NodeVisitor v) { 59 | v.visit(this); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/GeneratorExpressionLoop.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | /** 10 | */ 11 | public class GeneratorExpressionLoop extends ForInLoop { 12 | 13 | public GeneratorExpressionLoop() { 14 | } 15 | 16 | public GeneratorExpressionLoop(int pos) { 17 | super(pos); 18 | } 19 | 20 | public GeneratorExpressionLoop(int pos, int len) { 21 | super(pos, len); 22 | } 23 | 24 | /** 25 | * Returns whether the loop is a for-each loop 26 | */ 27 | @Override 28 | public boolean isForEach() { 29 | return false; 30 | } 31 | 32 | /** 33 | * Sets whether the loop is a for-each loop 34 | */ 35 | @Override 36 | public void setIsForEach(boolean isForEach) { 37 | throw new UnsupportedOperationException("this node type does not support for each"); 38 | } 39 | 40 | @Override 41 | public String toSource(int depth) { 42 | return makeIndent(depth) 43 | + " for " 44 | + (isForEach()?"each ":"") 45 | + "(" 46 | + iterator.toSource(0) 47 | + (isForOf()?" of ":" in ") 48 | + iteratedObject.toSource(0) 49 | + ")"; 50 | } 51 | 52 | /** 53 | * Visits the iterator expression and the iterated object expression. 54 | * There is no body-expression for this loop type. 55 | */ 56 | @Override 57 | public void visit(NodeVisitor v) { 58 | if (v.visit(this)) { 59 | iterator.visit(v); 60 | iteratedObject.visit(v); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/IdeErrorReporter.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript.ast; 10 | 11 | import org.mozilla.javascript.ErrorReporter; 12 | 13 | /** 14 | * This is interface defines a protocol for the reporting of 15 | * errors during JavaScript translation in IDE-mode. 16 | * If the {@link org.mozilla.javascript.Parser}'s error reporter is 17 | * set to an instance of this interface, then this interface's 18 | * {@link #warning} and {@link #error} methods are called instead 19 | * of the {@link org.mozilla.javascript.ErrorReporter} versions.

20 | * 21 | * These methods take a source char offset and a length. The 22 | * rationale is that in interactive IDE-type environments, the source 23 | * is available and the IDE will want to indicate where the error 24 | * occurred and how much code participates in it. The start and length 25 | * are generally chosen to fit within a single line, for readability, 26 | * but the client is free to use the AST to determine the affected 27 | * node(s) from the start position and change the error or warning's 28 | * display bounds.

29 | * 30 | * @author Steve Yegge 31 | */ 32 | public interface IdeErrorReporter extends ErrorReporter { 33 | 34 | /** 35 | * Report a warning.

36 | * 37 | * The implementing class may choose to ignore the warning 38 | * if it desires. 39 | * 40 | * @param message a {@code String} describing the warning 41 | * @param sourceName a {@code String} describing the JavaScript source 42 | * where the warning occured; typically a filename or URL 43 | * @param offset the warning's 0-indexed char position in the input stream 44 | * @param length the length of the region contributing to the warning 45 | */ 46 | void warning(String message, String sourceName, int offset, int length); 47 | 48 | /** 49 | * Report an error.

50 | * 51 | * The implementing class is free to throw an exception if 52 | * it desires.

53 | * 54 | * If execution has not yet begun, the JavaScript engine is 55 | * free to find additional errors rather than terminating 56 | * the translation. It will not execute a script that had 57 | * errors, however.

58 | * 59 | * @param message a String describing the error 60 | * @param sourceName a String describing the JavaScript source 61 | * where the error occured; typically a filename or URL 62 | * @param offset 0-indexed char position of the error in the input stream 63 | * @param length the length of the region contributing to the error 64 | */ 65 | void error(String message, String sourceName, int offset, int length); 66 | } 67 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/Label.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * AST node representing a label. It is a distinct node type so it can 13 | * record its length and position for code-processing tools. 14 | * Node type is {@link Token#LABEL}. 15 | */ 16 | public class Label extends Jump { 17 | 18 | private String name; 19 | 20 | { 21 | type = Token.LABEL; 22 | } 23 | 24 | public Label() { 25 | } 26 | 27 | public Label(int pos) { 28 | this(pos, -1); 29 | } 30 | 31 | public Label(int pos, int len) { 32 | // can't call super (Jump) for historical reasons 33 | position = pos; 34 | length = len; 35 | } 36 | 37 | public Label(int pos, int len, String name) { 38 | this(pos, len); 39 | setName(name); 40 | } 41 | 42 | /** 43 | * Returns the label text 44 | */ 45 | public String getName() { 46 | return name; 47 | } 48 | 49 | /** 50 | * Sets the label text 51 | * @throws IllegalArgumentException if name is {@code null} or the 52 | * empty string. 53 | */ 54 | public void setName(String name) { 55 | name = name == null ? null : name.trim(); 56 | if (name == null || "".equals(name)) 57 | throw new IllegalArgumentException("invalid label name"); 58 | this.name = name; 59 | } 60 | 61 | @Override 62 | public String toSource(int depth) { 63 | StringBuilder sb = new StringBuilder(); 64 | sb.append(makeIndent(depth)); 65 | sb.append(name); 66 | sb.append(":\n"); 67 | return sb.toString(); 68 | } 69 | 70 | /** 71 | * Visits this label. There are no children to visit. 72 | */ 73 | @Override 74 | public void visit(NodeVisitor v) { 75 | v.visit(this); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/Loop.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | /** 10 | * Abstract base type for loops. 11 | */ 12 | public abstract class Loop extends Scope { 13 | 14 | protected AstNode body; 15 | protected int lp = -1; 16 | protected int rp = -1; 17 | 18 | public Loop() { 19 | } 20 | 21 | public Loop(int pos) { 22 | super(pos); 23 | } 24 | 25 | public Loop(int pos, int len) { 26 | super(pos, len); 27 | } 28 | 29 | /** 30 | * Returns loop body 31 | */ 32 | public AstNode getBody() { 33 | return body; 34 | } 35 | 36 | /** 37 | * Sets loop body. Sets the parent of the body to this loop node, 38 | * and updates its offset to be relative. Extends the length of this 39 | * node to include the body. 40 | */ 41 | public void setBody(AstNode body) { 42 | this.body = body; 43 | int end = body.getPosition() + body.getLength(); 44 | this.setLength(end - this.getPosition()); 45 | body.setParent(this); 46 | } 47 | 48 | /** 49 | * Returns left paren position, -1 if missing 50 | */ 51 | public int getLp() { 52 | return lp; 53 | } 54 | 55 | /** 56 | * Sets left paren position 57 | */ 58 | public void setLp(int lp) { 59 | this.lp = lp; 60 | } 61 | 62 | /** 63 | * Returns right paren position, -1 if missing 64 | */ 65 | public int getRp() { 66 | return rp; 67 | } 68 | 69 | /** 70 | * Sets right paren position 71 | */ 72 | public void setRp(int rp) { 73 | this.rp = rp; 74 | } 75 | 76 | /** 77 | * Sets both paren positions 78 | */ 79 | public void setParens(int lp, int rp) { 80 | this.lp = lp; 81 | this.rp = rp; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/NodeVisitor.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | /** 10 | * Simple visitor interface for traversing the AST. The nodes are visited in 11 | * an arbitrary order. The visitor must cast nodes to the appropriate 12 | * type based on their token-type. 13 | */ 14 | public interface NodeVisitor { 15 | 16 | /** 17 | * Visits an AST node. 18 | * @param node the AST node. Will never visit an {@link AstRoot} node, 19 | * since the {@code AstRoot} is where the visiting begins. 20 | * @return {@code true} if the children should be visited. 21 | * If {@code false}, the subtree rooted at this node is skipped. 22 | * The {@code node} argument should never be {@code null} -- 23 | * the individual {@link AstNode} classes should skip any children 24 | * that are not present in the source when they invoke this method. 25 | */ 26 | boolean visit(AstNode node); 27 | } 28 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/NumberLiteral.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * AST node for a Number literal. Node type is {@link Token#NUMBER}. 13 | */ 14 | public class NumberLiteral extends AstNode { 15 | 16 | private String value; 17 | private double number; 18 | 19 | { 20 | type = Token.NUMBER; 21 | } 22 | 23 | public NumberLiteral() { 24 | } 25 | 26 | public NumberLiteral(int pos) { 27 | super(pos); 28 | } 29 | 30 | public NumberLiteral(int pos, int len) { 31 | super(pos, len); 32 | } 33 | 34 | /** 35 | * Constructor. Sets the length to the length of the {@code value} string. 36 | */ 37 | public NumberLiteral(int pos, String value) { 38 | super(pos); 39 | setValue(value); 40 | setLength(value.length()); 41 | } 42 | 43 | /** 44 | * Constructor. Sets the length to the length of the {@code value} string. 45 | */ 46 | public NumberLiteral(int pos, String value, double number) { 47 | this(pos, value); 48 | setDouble(number); 49 | } 50 | 51 | public NumberLiteral(double number) { 52 | setDouble(number); 53 | setValue(Double.toString(number)); 54 | } 55 | 56 | /** 57 | * Returns the node's string value (the original source token) 58 | */ 59 | public String getValue() { 60 | return value; 61 | } 62 | 63 | /** 64 | * Sets the node's value 65 | * @throws IllegalArgumentException} if value is {@code null} 66 | */ 67 | public void setValue(String value) { 68 | assertNotNull(value); 69 | this.value = value; 70 | } 71 | 72 | /** 73 | * Gets the {@code double} value. 74 | */ 75 | public double getNumber() { 76 | return number; 77 | } 78 | 79 | /** 80 | * Sets the node's {@code double} value. 81 | */ 82 | public void setNumber(double value) { 83 | number = value; 84 | } 85 | 86 | @Override 87 | public String toSource(int depth) { 88 | return makeIndent(depth) + (value == null ? "" : value); 89 | } 90 | 91 | /** 92 | * Visits this node. There are no children to visit. 93 | */ 94 | @Override 95 | public void visit(NodeVisitor v) { 96 | v.visit(this); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/ParenthesizedExpression.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * AST node for a parenthesized expression. 13 | * Node type is {@link Token#LP}. 14 | */ 15 | public class ParenthesizedExpression extends AstNode { 16 | 17 | private AstNode expression; 18 | 19 | { 20 | type = Token.LP; 21 | } 22 | 23 | public ParenthesizedExpression() { 24 | } 25 | 26 | public ParenthesizedExpression(int pos) { 27 | super(pos); 28 | } 29 | 30 | public ParenthesizedExpression(int pos, int len) { 31 | super(pos, len); 32 | } 33 | 34 | public ParenthesizedExpression(AstNode expr) { 35 | this(expr != null ? expr.getPosition() : 0, 36 | expr != null ? expr.getLength() : 1, 37 | expr); 38 | } 39 | 40 | public ParenthesizedExpression(int pos, int len, AstNode expr) { 41 | super(pos, len); 42 | setExpression(expr); 43 | } 44 | 45 | /** 46 | * Returns the expression between the parens 47 | */ 48 | public AstNode getExpression() { 49 | return expression; 50 | } 51 | 52 | /** 53 | * Sets the expression between the parens, and sets the parent 54 | * to this node. 55 | * @param expression the expression between the parens 56 | * @throws IllegalArgumentException} if expression is {@code null} 57 | */ 58 | public void setExpression(AstNode expression) { 59 | assertNotNull(expression); 60 | this.expression = expression; 61 | expression.setParent(this); 62 | } 63 | 64 | @Override 65 | public String toSource(int depth) { 66 | return makeIndent(depth) + "(" + expression.toSource(0) + ")"; 67 | } 68 | 69 | /** 70 | * Visits this node, then the child expression. 71 | */ 72 | @Override 73 | public void visit(NodeVisitor v) { 74 | if (v.visit(this)) { 75 | expression.visit(v); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/ParseProblem.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | /** 10 | * Encapsulates information for a JavaScript parse error or warning. 11 | */ 12 | public class ParseProblem { 13 | 14 | public static enum Type {Error, Warning} 15 | 16 | private Type type; 17 | private String message; 18 | private String sourceName; 19 | private int offset; 20 | private int length; 21 | 22 | /** 23 | * Constructs a new ParseProblem. 24 | */ 25 | public ParseProblem(ParseProblem.Type type, String message, 26 | String sourceName, int offset, int length) { 27 | setType(type); 28 | setMessage(message); 29 | setSourceName(sourceName); 30 | setFileOffset(offset); 31 | setLength(length); 32 | } 33 | 34 | public ParseProblem.Type getType() { 35 | return type; 36 | } 37 | 38 | public void setType(ParseProblem.Type type) { 39 | this.type = type; 40 | } 41 | 42 | public String getMessage() { 43 | return message; 44 | } 45 | 46 | public void setMessage(String msg) { 47 | this.message = msg; 48 | } 49 | 50 | public String getSourceName() { 51 | return sourceName; 52 | } 53 | 54 | public void setSourceName(String name) { 55 | this.sourceName = name; 56 | } 57 | 58 | public int getFileOffset() { 59 | return offset; 60 | } 61 | 62 | public void setFileOffset(int offset) { 63 | this.offset = offset; 64 | } 65 | 66 | public int getLength() { 67 | return length; 68 | } 69 | 70 | public void setLength(int length) { 71 | this.length = length; 72 | } 73 | 74 | @Override 75 | public String toString() { 76 | StringBuilder sb = new StringBuilder(200); 77 | sb.append(sourceName).append(":"); 78 | sb.append("offset=").append(offset).append(","); 79 | sb.append("length=").append(length).append(","); 80 | sb.append(type == Type.Error ? "error: " : "warning: "); 81 | sb.append(message); 82 | return sb.toString(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/PropertyGet.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * AST node for the '.' operator. Node type is {@link Token#GETPROP}. 13 | */ 14 | public class PropertyGet extends InfixExpression { 15 | 16 | { 17 | type = Token.GETPROP; 18 | } 19 | 20 | public PropertyGet() { 21 | } 22 | 23 | public PropertyGet(int pos) { 24 | super(pos); 25 | } 26 | 27 | public PropertyGet(int pos, int len) { 28 | super(pos, len); 29 | } 30 | 31 | public PropertyGet(int pos, int len, AstNode target, Name property) { 32 | super(pos, len, target, property); 33 | } 34 | 35 | /** 36 | * Constructor. Updates bounds to include left ({@code target}) and 37 | * right ({@code property}) nodes. 38 | */ 39 | public PropertyGet(AstNode target, Name property) { 40 | super(target, property); 41 | } 42 | 43 | public PropertyGet(AstNode target, Name property, int dotPosition) { 44 | super(Token.GETPROP, target, property, dotPosition); 45 | } 46 | 47 | /** 48 | * Returns the object on which the property is being fetched. 49 | * Should never be {@code null}. 50 | */ 51 | public AstNode getTarget() { 52 | return getLeft(); 53 | } 54 | 55 | /** 56 | * Sets target object, and sets its parent to this node. 57 | * @param target expression evaluating to the object upon which 58 | * to do the property lookup 59 | * @throws IllegalArgumentException} if {@code target} is {@code null} 60 | */ 61 | public void setTarget(AstNode target) { 62 | setLeft(target); 63 | } 64 | 65 | /** 66 | * Returns the property being accessed. 67 | */ 68 | public Name getProperty() { 69 | return (Name)getRight(); 70 | } 71 | 72 | /** 73 | * Sets the property being accessed, and sets its parent to this node. 74 | * @throws IllegalArgumentException} if {@code property} is {@code null} 75 | */ 76 | public void setProperty(Name property) { 77 | setRight(property); 78 | } 79 | 80 | @Override 81 | public String toSource(int depth) { 82 | StringBuilder sb = new StringBuilder(); 83 | sb.append(makeIndent(depth)); 84 | sb.append(getLeft().toSource(0)); 85 | sb.append("."); 86 | sb.append(getRight().toSource(0)); 87 | return sb.toString(); 88 | } 89 | 90 | /** 91 | * Visits this node, the target expression, and the property name. 92 | */ 93 | @Override 94 | public void visit(NodeVisitor v) { 95 | if (v.visit(this)) { 96 | getTarget().visit(v); 97 | getProperty().visit(v); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/RegExpLiteral.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * AST node for a RegExp literal. 13 | * Node type is {@link Token#REGEXP}. 14 | */ 15 | public class RegExpLiteral extends AstNode { 16 | 17 | private String value; 18 | private String flags; 19 | 20 | { 21 | type = Token.REGEXP; 22 | } 23 | 24 | public RegExpLiteral() { 25 | } 26 | 27 | public RegExpLiteral(int pos) { 28 | super(pos); 29 | } 30 | 31 | public RegExpLiteral(int pos, int len) { 32 | super(pos, len); 33 | } 34 | 35 | /** 36 | * Returns the regexp string without delimiters 37 | */ 38 | public String getValue() { 39 | return value; 40 | } 41 | 42 | /** 43 | * Sets the regexp string without delimiters 44 | * @throws IllegalArgumentException} if value is {@code null} 45 | */ 46 | public void setValue(String value) { 47 | assertNotNull(value); 48 | this.value = value; 49 | } 50 | 51 | /** 52 | * Returns regexp flags, {@code null} or "" if no flags specified 53 | */ 54 | public String getFlags() { 55 | return flags; 56 | } 57 | 58 | /** 59 | * Sets regexp flags. Can be {@code null} or "". 60 | */ 61 | public void setFlags(String flags) { 62 | this.flags = flags; 63 | } 64 | 65 | @Override 66 | public String toSource(int depth) { 67 | return makeIndent(depth) + "/" + value + "/" 68 | + (flags == null ? "" : flags); 69 | } 70 | 71 | /** 72 | * Visits this node. There are no children to visit. 73 | */ 74 | @Override 75 | public void visit(NodeVisitor v) { 76 | v.visit(this); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/ReturnStatement.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * Return statement. Node type is {@link Token#RETURN}. 13 | * 14 | *

ReturnStatement :
15 |  *      return [no LineTerminator here] [Expression] ;
16 | */ 17 | public class ReturnStatement extends AstNode { 18 | 19 | private AstNode returnValue; 20 | 21 | { 22 | type = Token.RETURN; 23 | } 24 | 25 | public ReturnStatement() { 26 | } 27 | 28 | public ReturnStatement(int pos) { 29 | super(pos); 30 | } 31 | 32 | public ReturnStatement(int pos, int len) { 33 | super(pos, len); 34 | } 35 | 36 | public ReturnStatement(int pos, int len, AstNode returnValue) { 37 | super(pos, len); 38 | setReturnValue(returnValue); 39 | } 40 | 41 | /** 42 | * Returns return value, {@code null} if return value is void 43 | */ 44 | public AstNode getReturnValue() { 45 | return returnValue; 46 | } 47 | 48 | /** 49 | * Sets return value expression, and sets its parent to this node. 50 | * Can be {@code null}. 51 | */ 52 | public void setReturnValue(AstNode returnValue) { 53 | this.returnValue = returnValue; 54 | if (returnValue != null) 55 | returnValue.setParent(this); 56 | } 57 | 58 | @Override 59 | public String toSource(int depth) { 60 | StringBuilder sb = new StringBuilder(); 61 | sb.append(makeIndent(depth)); 62 | sb.append("return"); 63 | if (returnValue != null) { 64 | sb.append(" "); 65 | sb.append(returnValue.toSource(0)); 66 | } 67 | sb.append(";\n"); 68 | return sb.toString(); 69 | } 70 | 71 | /** 72 | * Visits this node, then the return value if specified. 73 | */ 74 | @Override 75 | public void visit(NodeVisitor v) { 76 | if (v.visit(this) && returnValue != null) { 77 | returnValue.visit(v); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/StringLiteral.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.ScriptRuntime; 10 | import org.mozilla.javascript.Token; 11 | 12 | /** 13 | * AST node for a single- or double-quoted string literal. 14 | * Node type is {@link Token#STRING}. 15 | */ 16 | public class StringLiteral extends AstNode { 17 | 18 | private String value; 19 | private char quoteChar; 20 | 21 | { 22 | type = Token.STRING; 23 | } 24 | 25 | public StringLiteral() { 26 | } 27 | 28 | public StringLiteral(int pos) { 29 | super(pos); 30 | } 31 | 32 | /** 33 | * Creates a string literal node at the specified position. 34 | * @param len the length including the enclosing quotes 35 | */ 36 | public StringLiteral(int pos, int len) { 37 | super(pos, len); 38 | } 39 | 40 | /** 41 | * Returns the node's value: the parsed string without the enclosing quotes 42 | * @return the node's value, a {@link String} of unescaped characters 43 | * that includes the delimiter quotes. 44 | */ 45 | public String getValue() { 46 | return value; 47 | } 48 | 49 | /** 50 | * Returns the string value, optionally including the enclosing quotes. 51 | */ 52 | public String getValue(boolean includeQuotes) { 53 | if (!includeQuotes) 54 | return value; 55 | return quoteChar + value + quoteChar; 56 | } 57 | 58 | /** 59 | * Sets the node's value. Do not include the enclosing quotes. 60 | * @param value the node's value 61 | * @throws IllegalArgumentException} if value is {@code null} 62 | */ 63 | public void setValue(String value) { 64 | assertNotNull(value); 65 | this.value = value; 66 | } 67 | 68 | /** 69 | * Returns the character used as the delimiter for this string. 70 | */ 71 | public char getQuoteCharacter() { 72 | return quoteChar; 73 | } 74 | 75 | public void setQuoteCharacter(char c) { 76 | quoteChar = c; 77 | } 78 | 79 | @Override 80 | public String toSource(int depth) { 81 | return new StringBuilder(makeIndent(depth)) 82 | .append(quoteChar) 83 | .append(ScriptRuntime.escapeString(value, quoteChar)) 84 | .append(quoteChar) 85 | .toString(); 86 | } 87 | 88 | /** 89 | * Visits this node. There are no children to visit. 90 | */ 91 | @Override 92 | public void visit(NodeVisitor v) { 93 | v.visit(this); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/TaggedTemplateLiteral.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.ast; 6 | 7 | import org.mozilla.javascript.Token; 8 | 9 | /** 10 | * AST node for a Tagged Template Literal. 11 | * 12 | *

Node type is {@link Token#TAGGED_TEMPLATE_LITERAL}. 13 | */ 14 | public class TaggedTemplateLiteral extends AstNode { 15 | 16 | private AstNode target; 17 | private AstNode templateLiteral; 18 | 19 | { 20 | type = Token.TAGGED_TEMPLATE_LITERAL; 21 | } 22 | 23 | public TaggedTemplateLiteral() {} 24 | 25 | public TaggedTemplateLiteral(int pos) { 26 | super(pos); 27 | } 28 | 29 | public TaggedTemplateLiteral(int pos, int len) { 30 | super(pos, len); 31 | } 32 | 33 | public AstNode getTarget() { 34 | return target; 35 | } 36 | 37 | public void setTarget(AstNode target) { 38 | this.target = target; 39 | } 40 | 41 | public AstNode getTemplateLiteral() { 42 | return templateLiteral; 43 | } 44 | 45 | public void setTemplateLiteral(AstNode templateLiteral) { 46 | this.templateLiteral = templateLiteral; 47 | } 48 | 49 | @Override 50 | public String toSource(int depth) { 51 | StringBuilder sb = new StringBuilder(); 52 | sb.append(makeIndent(depth)); 53 | sb.append(target.toSource(0)); 54 | sb.append(templateLiteral.toSource(0)); 55 | return sb.toString(); 56 | } 57 | 58 | /** Visits this node. */ 59 | @Override 60 | public void visit(NodeVisitor v) { 61 | if (v.visit(this)) { 62 | target.visit(v); 63 | templateLiteral.visit(v); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/TemplateCharacters.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.ast; 6 | 7 | import org.mozilla.javascript.Token; 8 | 9 | /** 10 | * AST node for Template Literal Characters. 11 | * 12 | *

Node type is {@link Token#TEMPLATE_CHARS}. 13 | */ 14 | public class TemplateCharacters extends AstNode { 15 | 16 | private String value; 17 | private String rawValue; 18 | 19 | { 20 | type = Token.TEMPLATE_CHARS; 21 | } 22 | 23 | public TemplateCharacters() {} 24 | 25 | public TemplateCharacters(int pos) { 26 | super(pos); 27 | } 28 | 29 | public TemplateCharacters(int pos, int len) { 30 | super(pos, len); 31 | } 32 | 33 | /** 34 | * Returns the node's value: the parsed template-literal-value (QV) 35 | * 36 | * @return the node's value 37 | */ 38 | public String getValue() { 39 | return value; 40 | } 41 | 42 | /** 43 | * Sets the node's value. Can be null in case of illegal escape sequences, which are allowed in 44 | * Template Literals but will have an undefined cooked value 45 | * 46 | * @param value the node's value 47 | */ 48 | public void setValue(String value) { 49 | this.value = value; 50 | } 51 | 52 | /** 53 | * Returns the node's raw-value: the parsed template-literal-raw-value (QRV) 54 | * 55 | * @return the node's raw-value 56 | */ 57 | public String getRawValue() { 58 | return rawValue; 59 | } 60 | 61 | /** 62 | * Sets the node's raw-value. 63 | * 64 | * @param rawValue the node's raw-value 65 | * @throws IllegalArgumentException} if rawValue is {@code null} 66 | */ 67 | public void setRawValue(String rawValue) { 68 | assertNotNull(rawValue); 69 | this.rawValue = rawValue; 70 | } 71 | 72 | @Override 73 | public String toSource(int depth) { 74 | return new StringBuilder(makeIndent(depth)).append(rawValue).toString(); 75 | } 76 | 77 | /** Visits this node. There are no children to visit. */ 78 | @Override 79 | public void visit(NodeVisitor v) { 80 | v.visit(this); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/ThrowStatement.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * Throw statement. Node type is {@link Token#THROW}. 13 | * 14 | *

ThrowStatement :
15 |  *      throw [no LineTerminator here] Expression ;
16 | */ 17 | public class ThrowStatement extends AstNode { 18 | 19 | private AstNode expression; 20 | 21 | { 22 | type = Token.THROW; 23 | } 24 | 25 | public ThrowStatement() { 26 | } 27 | 28 | public ThrowStatement(int pos) { 29 | super(pos); 30 | } 31 | 32 | public ThrowStatement(int pos, int len) { 33 | super(pos, len); 34 | } 35 | 36 | public ThrowStatement(AstNode expr) { 37 | setExpression(expr); 38 | } 39 | 40 | public ThrowStatement(int pos, AstNode expr) { 41 | super(pos, expr.getLength()); 42 | setExpression(expr); 43 | } 44 | 45 | public ThrowStatement(int pos, int len, AstNode expr) { 46 | super(pos, len); 47 | setExpression(expr); 48 | } 49 | 50 | /** 51 | * Returns the expression being thrown 52 | */ 53 | public AstNode getExpression() { 54 | return expression; 55 | } 56 | 57 | /** 58 | * Sets the expression being thrown, and sets its parent 59 | * to this node. 60 | * @throws IllegalArgumentException} if expression is {@code null} 61 | */ 62 | public void setExpression(AstNode expression) { 63 | assertNotNull(expression); 64 | this.expression = expression; 65 | expression.setParent(this); 66 | } 67 | 68 | @Override 69 | public String toSource(int depth) { 70 | StringBuilder sb = new StringBuilder(); 71 | sb.append(makeIndent(depth)); 72 | sb.append("throw"); 73 | sb.append(" "); 74 | sb.append(expression.toSource(0)); 75 | sb.append(";\n"); 76 | return sb.toString(); 77 | } 78 | 79 | /** 80 | * Visits this node, then the thrown expression. 81 | */ 82 | @Override 83 | public void visit(NodeVisitor v) { 84 | if (v.visit(this)) { 85 | expression.visit(v); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/WhileLoop.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * While statement. Node type is {@link Token#WHILE}. 13 | * 14 | *
WhileStatement:
15 |  *     while ( Expression ) Statement
16 | */ 17 | public class WhileLoop extends Loop { 18 | 19 | private AstNode condition; 20 | 21 | { 22 | type = Token.WHILE; 23 | } 24 | 25 | public WhileLoop() { 26 | } 27 | 28 | public WhileLoop(int pos) { 29 | super(pos); 30 | } 31 | 32 | public WhileLoop(int pos, int len) { 33 | super(pos, len); 34 | } 35 | 36 | /** 37 | * Returns loop condition 38 | */ 39 | public AstNode getCondition() { 40 | return condition; 41 | } 42 | 43 | /** 44 | * Sets loop condition 45 | * @throws IllegalArgumentException} if condition is {@code null} 46 | */ 47 | public void setCondition(AstNode condition) { 48 | assertNotNull(condition); 49 | this.condition = condition; 50 | condition.setParent(this); 51 | } 52 | 53 | @Override 54 | public String toSource(int depth) { 55 | StringBuilder sb = new StringBuilder(); 56 | sb.append(makeIndent(depth)); 57 | sb.append("while ("); 58 | sb.append(condition.toSource(0)); 59 | sb.append(") "); 60 | if(this.getInlineComment() != null) { 61 | sb.append(this.getInlineComment().toSource(depth + 1)).append("\n"); 62 | } 63 | if (body.getType() == Token.BLOCK) { 64 | sb.append(body.toSource(depth).trim()); 65 | sb.append("\n"); 66 | } else { 67 | if(this.getInlineComment() == null) { 68 | sb.append("\n"); 69 | } 70 | sb.append(body.toSource(depth+1)); 71 | } 72 | return sb.toString(); 73 | } 74 | 75 | /** 76 | * Visits this node, the condition, then the body. 77 | */ 78 | @Override 79 | public void visit(NodeVisitor v) { 80 | if (v.visit(this)) { 81 | condition.visit(v); 82 | body.visit(v); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/XmlDotQuery.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * AST node representing an E4X {@code foo.(bar)} query expression. 13 | * The node type (operator) is {@link Token#DOTQUERY}. 14 | * Its {@code getLeft} node is the target ("foo" in the example), 15 | * and the {@code getRight} node is the filter expression node.

16 | * 17 | * This class exists separately from {@link InfixExpression} largely because it 18 | * has different printing needs. The position of the left paren is just after 19 | * the dot (operator) position, and the right paren is the final position in the 20 | * bounds of the node. If the right paren is missing, the node ends at the end 21 | * of the filter expression. 22 | */ 23 | public class XmlDotQuery extends InfixExpression { 24 | 25 | private int rp = -1; 26 | 27 | { 28 | type = Token.DOTQUERY; 29 | } 30 | 31 | public XmlDotQuery() { 32 | } 33 | 34 | public XmlDotQuery(int pos) { 35 | super(pos); 36 | } 37 | 38 | public XmlDotQuery(int pos, int len) { 39 | super(pos, len); 40 | } 41 | 42 | /** 43 | * Returns right-paren position, -1 if missing.

44 | * 45 | * Note that the left-paren is automatically the character 46 | * immediately after the "." in the operator - no whitespace is 47 | * permitted between the dot and lp by the scanner. 48 | */ 49 | public int getRp() { 50 | return rp; 51 | } 52 | 53 | /** 54 | * Sets right-paren position 55 | */ 56 | public void setRp(int rp) { 57 | this.rp = rp; 58 | } 59 | 60 | @Override 61 | public String toSource(int depth) { 62 | StringBuilder sb = new StringBuilder(); 63 | sb.append(makeIndent(depth)); 64 | sb.append(getLeft().toSource(0)); 65 | sb.append(".("); 66 | sb.append(getRight().toSource(0)); 67 | sb.append(")"); 68 | return sb.toString(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/XmlExpression.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * AST node for an embedded JavaScript expression within an E4X XML literal. 13 | * Node type, like {@link XmlLiteral}, is {@link Token#XML}. The node length 14 | * includes the curly braces. 15 | */ 16 | public class XmlExpression extends XmlFragment { 17 | 18 | private AstNode expression; 19 | private boolean isXmlAttribute; 20 | 21 | public XmlExpression() { 22 | } 23 | 24 | public XmlExpression(int pos) { 25 | super(pos); 26 | } 27 | 28 | public XmlExpression(int pos, int len) { 29 | super(pos, len); 30 | } 31 | 32 | public XmlExpression(int pos, AstNode expr) { 33 | super(pos); 34 | setExpression(expr); 35 | } 36 | 37 | /** 38 | * Returns the expression embedded in {} 39 | */ 40 | public AstNode getExpression() { 41 | return expression; 42 | } 43 | 44 | /** 45 | * Sets the expression embedded in {}, and sets its parent to this node. 46 | * @throws IllegalArgumentException if {@code expression} is {@code null} 47 | */ 48 | public void setExpression(AstNode expression) { 49 | assertNotNull(expression); 50 | this.expression = expression; 51 | expression.setParent(this); 52 | } 53 | 54 | /** 55 | * Returns whether this is part of an xml attribute value 56 | */ 57 | public boolean isXmlAttribute() { 58 | return isXmlAttribute; 59 | } 60 | 61 | /** 62 | * Sets whether this is part of an xml attribute value 63 | */ 64 | public void setIsXmlAttribute(boolean isXmlAttribute) { 65 | this.isXmlAttribute = isXmlAttribute; 66 | } 67 | 68 | @Override 69 | public String toSource(int depth) { 70 | return makeIndent(depth) + "{" + expression.toSource(depth) + "}"; 71 | } 72 | 73 | /** 74 | * Visits this node, then the child expression. 75 | */ 76 | @Override 77 | public void visit(NodeVisitor v) { 78 | if (v.visit(this)) { 79 | expression.visit(v); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/XmlFragment.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * Abstract base type for components that comprise an {@link XmlLiteral} 13 | * object. Node type is {@link Token#XML}. 14 | */ 15 | public abstract class XmlFragment extends AstNode { 16 | 17 | { 18 | type = Token.XML; 19 | } 20 | 21 | public XmlFragment() { 22 | } 23 | 24 | public XmlFragment(int pos) { 25 | super(pos); 26 | } 27 | 28 | public XmlFragment(int pos, int len) { 29 | super(pos, len); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/XmlLiteral.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | import org.mozilla.javascript.Token; 13 | 14 | /** 15 | * AST node for an E4X (Ecma-357) embedded XML literal. Node type is 16 | * {@link Token#XML}. The parser generates a simple list of strings and 17 | * expressions. In the future we may parse the XML and produce a richer set of 18 | * nodes, but for now it's just a set of expressions evaluated to produce a 19 | * string to pass to the {@code XML} constructor function. 20 | */ 21 | public class XmlLiteral extends AstNode { 22 | 23 | private List fragments = new ArrayList(); 24 | 25 | { 26 | type = Token.XML; 27 | } 28 | 29 | public XmlLiteral() { 30 | } 31 | 32 | public XmlLiteral(int pos) { 33 | super(pos); 34 | } 35 | 36 | public XmlLiteral(int pos, int len) { 37 | super(pos, len); 38 | } 39 | 40 | /** 41 | * Returns fragment list - a list of expression nodes. 42 | */ 43 | public List getFragments() { 44 | return fragments; 45 | } 46 | 47 | /** 48 | * Sets fragment list, removing any existing fragments first. 49 | * Sets the parent pointer for each fragment in the list to this node. 50 | * @param fragments fragment list. Replaces any existing fragments. 51 | * @throws IllegalArgumentException} if {@code fragments} is {@code null} 52 | */ 53 | public void setFragments(List fragments) { 54 | assertNotNull(fragments); 55 | this.fragments.clear(); 56 | for (XmlFragment fragment : fragments) 57 | addFragment(fragment); 58 | } 59 | 60 | /** 61 | * Adds a fragment to the fragment list. Sets its parent to this node. 62 | * @throws IllegalArgumentException} if {@code fragment} is {@code null} 63 | */ 64 | public void addFragment(XmlFragment fragment) { 65 | assertNotNull(fragment); 66 | fragments.add(fragment); 67 | fragment.setParent(this); 68 | } 69 | 70 | @Override 71 | public String toSource(int depth) { 72 | StringBuilder sb = new StringBuilder(250); 73 | for (XmlFragment frag : fragments) { 74 | sb.append(frag.toSource(0)); 75 | } 76 | return sb.toString(); 77 | } 78 | 79 | /** 80 | * Visits this node, then visits each child fragment in lexical order. 81 | */ 82 | @Override 83 | public void visit(NodeVisitor v) { 84 | if (v.visit(this)) { 85 | for (XmlFragment frag : fragments) { 86 | frag.visit(v); 87 | } 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/XmlPropRef.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * AST node for an E4X XML {@code [expr]} property-ref expression. 13 | * The node type is {@link Token#REF_NAME}.
14 | * 15 | * Syntax: 16 | * 17 | *

 @opt ns:: opt name
18 | * 19 | * Examples include {@code name}, {@code ns::name}, {@code ns::*}, 20 | * {@code *::name}, {@code *::*}, {@code @attr}, {@code @ns::attr}, 21 | * {@code @ns::*}, {@code @*::attr}, {@code @*::*} and {@code @*}.

22 | * 23 | * The node starts at the {@code @} token, if present. Otherwise it starts 24 | * at the namespace name. The node bounds extend through the closing 25 | * right-bracket, or if it is missing due to a syntax error, through the 26 | * end of the index expression. 27 | */ 28 | public class XmlPropRef extends XmlRef { 29 | 30 | private Name propName; 31 | 32 | { 33 | type = Token.REF_NAME; 34 | } 35 | 36 | public XmlPropRef() { 37 | } 38 | 39 | public XmlPropRef(int pos) { 40 | super(pos); 41 | } 42 | 43 | public XmlPropRef(int pos, int len) { 44 | super(pos, len); 45 | } 46 | 47 | /** 48 | * Returns property name. 49 | */ 50 | public Name getPropName() { 51 | return propName; 52 | } 53 | 54 | /** 55 | * Sets property name, and sets its parent to this node. 56 | * @throws IllegalArgumentException if {@code propName} is {@code null} 57 | */ 58 | public void setPropName(Name propName) { 59 | assertNotNull(propName); 60 | this.propName = propName; 61 | propName.setParent(this); 62 | } 63 | 64 | @Override 65 | public String toSource(int depth) { 66 | StringBuilder sb = new StringBuilder(); 67 | sb.append(makeIndent(depth)); 68 | if (isAttributeAccess()) { 69 | sb.append("@"); 70 | } 71 | if (namespace != null) { 72 | sb.append(namespace.toSource(0)); 73 | sb.append("::"); 74 | } 75 | sb.append(propName.toSource(0)); 76 | return sb.toString(); 77 | } 78 | 79 | /** 80 | * Visits this node, then the namespace if present, then the property name. 81 | */ 82 | @Override 83 | public void visit(NodeVisitor v) { 84 | if (v.visit(this)) { 85 | if (namespace != null) { 86 | namespace.visit(v); 87 | } 88 | propName.visit(v); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/XmlString.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | /** 10 | * AST node for an XML-text-only component of an XML literal expression. This 11 | * node differs from a {@link StringLiteral} in that it does not have quotes for 12 | * delimiters. 13 | */ 14 | public class XmlString extends XmlFragment { 15 | 16 | private String xml; 17 | 18 | public XmlString() { 19 | } 20 | 21 | public XmlString(int pos) { 22 | super(pos); 23 | } 24 | 25 | public XmlString(int pos, String s) { 26 | super(pos); 27 | setXml(s); 28 | } 29 | 30 | /** 31 | * Sets the string for this XML component. Sets the length of the 32 | * component to the length of the passed string. 33 | * @param s a string of xml text 34 | * @throws IllegalArgumentException} if {@code s} is {@code null} 35 | */ 36 | public void setXml(String s) { 37 | assertNotNull(s); 38 | xml = s; 39 | setLength(s.length()); 40 | } 41 | 42 | /** 43 | * Returns the xml string for this component. 44 | * Note that it may not be well-formed XML; it is a fragment. 45 | */ 46 | public String getXml() { 47 | return xml; 48 | } 49 | 50 | @Override 51 | public String toSource(int depth) { 52 | return makeIndent(depth) + xml; 53 | } 54 | 55 | /** 56 | * Visits this node. There are no children to visit. 57 | */ 58 | @Override 59 | public void visit(NodeVisitor v) { 60 | v.visit(this); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/ast/Yield.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.ast; 8 | 9 | import org.mozilla.javascript.Token; 10 | 11 | /** 12 | * AST node for JavaScript 1.7 {@code yield} expression or statement. 13 | * Node type is {@link Token#YIELD}. 14 | * 15 | *

Yield :
16 |  *   yield [no LineTerminator here] [non-paren Expression] ;
17 | */ 18 | public class Yield extends AstNode { 19 | 20 | private AstNode value; 21 | 22 | public Yield() { 23 | type = Token.YIELD; 24 | } 25 | 26 | public Yield(int pos) { 27 | super(pos); 28 | type = Token.YIELD; 29 | } 30 | 31 | public Yield(int pos, int len) { 32 | super(pos, len); 33 | type = Token.YIELD; 34 | } 35 | 36 | public Yield(int pos, int len, AstNode value, boolean isStar) { 37 | super(pos, len); 38 | type = isStar ? Token.YIELD_STAR : Token.YIELD; 39 | setValue(value); 40 | } 41 | 42 | /** 43 | * Returns yielded expression, {@code null} if none 44 | */ 45 | public AstNode getValue() { 46 | return value; 47 | } 48 | 49 | /** 50 | * Sets yielded expression, and sets its parent to this node. 51 | * @param expr the value to yield. Can be {@code null}. 52 | */ 53 | public void setValue(AstNode expr) { 54 | this.value = expr; 55 | if (expr != null) 56 | expr.setParent(this); 57 | } 58 | 59 | @Override 60 | public String toSource(int depth) { 61 | return value == null 62 | ? "yield" 63 | : "yield " + value.toSource(0); 64 | } 65 | 66 | /** 67 | * Visits this node, and if present, the yielded value. 68 | */ 69 | @Override 70 | public void visit(NodeVisitor v) { 71 | if (v.visit(this) && value != null) { 72 | value.visit(v); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/commonjs/module/ModuleScope.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.commonjs.module; 6 | 7 | import java.net.URI; 8 | 9 | import org.mozilla.javascript.Scriptable; 10 | import org.mozilla.javascript.TopLevel; 11 | 12 | /** 13 | * A top-level module scope. This class provides methods to retrieve the 14 | * module's source and base URIs in order to resolve relative module IDs 15 | * and check sandbox constraints. 16 | */ 17 | public class ModuleScope extends TopLevel { 18 | private static final long serialVersionUID = 1L; 19 | private final URI uri; 20 | private final URI base; 21 | 22 | public ModuleScope(Scriptable prototype, URI uri, URI base) { 23 | this.uri = uri; 24 | this.base = base; 25 | setPrototype(prototype); 26 | cacheBuiltins(prototype, false); 27 | } 28 | 29 | public URI getUri() { 30 | return uri; 31 | } 32 | 33 | public URI getBase() { 34 | return base; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/commonjs/module/ModuleScript.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.commonjs.module; 6 | 7 | import java.io.Serializable; 8 | import java.net.URI; 9 | 10 | import org.mozilla.javascript.Script; 11 | 12 | /** 13 | * Represents a compiled CommonJS module script. The {@link Require} functions 14 | * use them and obtain them through a {@link ModuleScriptProvider}. Instances 15 | * are immutable. 16 | * @author Attila Szegedi 17 | * @version $Id: ModuleScript.java,v 1.3 2011/04/07 20:26:11 hannes%helma.at Exp $ 18 | */ 19 | public class ModuleScript implements Serializable { 20 | private static final long serialVersionUID = 1L; 21 | private final Script script; 22 | private final URI uri; 23 | private final URI base; 24 | 25 | /** 26 | * Creates a new CommonJS module. 27 | * @param script the script representing the code of the module. 28 | * @param uri the URI of the module. 29 | * @param base the base URI, or null. 30 | */ 31 | public ModuleScript(Script script, URI uri, URI base) { 32 | this.script = script; 33 | this.uri = uri; 34 | this.base = base; 35 | } 36 | 37 | /** 38 | * Returns the script object representing the code of the module. 39 | * @return the script object representing the code of the module. 40 | */ 41 | public Script getScript(){ 42 | return script; 43 | } 44 | 45 | /** 46 | * Returns the URI of the module. 47 | * @return the URI of the module. 48 | */ 49 | public URI getUri() { 50 | return uri; 51 | } 52 | 53 | /** 54 | * Returns the base URI from which this module source was loaded, or null 55 | * if it was loaded from an absolute URI. 56 | * @return the base URI, or null. 57 | */ 58 | public URI getBase() { 59 | return base; 60 | } 61 | 62 | /** 63 | * Returns true if this script has a base URI and has a source URI that 64 | * is contained within that base URI. 65 | * @return true if this script is contained within its sandbox base URI. 66 | */ 67 | public boolean isSandboxed() { 68 | return base != null 69 | && uri != null 70 | && !base.relativize(uri).isAbsolute(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/commonjs/module/ModuleScriptProvider.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.commonjs.module; 6 | 7 | import java.net.URI; 8 | 9 | import org.mozilla.javascript.Context; 10 | import org.mozilla.javascript.Scriptable; 11 | 12 | /** 13 | * Should be implemented by Rhino embeddings to allow the require() function to 14 | * obtain {@link ModuleScript} objects. We provide two default implementations, 15 | * but you can of course roll your own if they don't suit your needs. 16 | * @author Attila Szegedi 17 | * @version $Id: ModuleScriptProvider.java,v 1.4 2011/04/07 20:26:11 hannes%helma.at Exp $ 18 | */ 19 | public interface ModuleScriptProvider 20 | { 21 | /** 22 | * Returns a module script. It should attempt to load the module script if 23 | * it is not already available to it, or return an already loaded module 24 | * script instance if it is available to it. 25 | * @param cx current context. Can be used to compile module scripts. 26 | * @param moduleId the ID of the module. An implementation must only accept 27 | * an absolute ID, starting with a term. 28 | * @param moduleUri the URI of the module. If this is not null, resolution 29 | * of moduleId is bypassed and the script is directly loaded 30 | * from moduleUri 31 | * @param baseUri the module path base URI from which moduleUri 32 | * was derived. 33 | * @param paths the value of the require() function's "paths" attribute. If 34 | * the require() function is sandboxed, it will be null, otherwise it will 35 | * be a JavaScript Array object. It is up to the provider implementation 36 | * whether and how it wants to honor the contents of the array. 37 | * @return a module script representing the compiled code of the module. 38 | * Null should be returned if the script could not found. 39 | * @throws Exception if there was an unrecoverable problem obtaining the 40 | * script 41 | * @throws IllegalArgumentException if the module ID is syntactically not a 42 | * valid absolute module identifier. 43 | */ 44 | public ModuleScript getModuleScript(Context cx, String moduleId, 45 | URI moduleUri, URI baseUri, Scriptable paths) 46 | throws Exception; 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/commonjs/module/package.html: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | Provides the public API for the CommonJS Modules/1.1 implementation. Note that 8 | it is completely up to the embedder of the Rhino library to provide an 9 | implementation for ModuleProvider. However, to help you get 10 | started, the "support" package offers default implementations of module 11 | providers that are likely sufficient for most purposes, and can be further 12 | extended. 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/commonjs/module/provider/DefaultUrlConnectionExpiryCalculator.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.commonjs.module.provider; 6 | 7 | import java.io.Serializable; 8 | import java.net.URLConnection; 9 | 10 | /** 11 | * The default heuristic for calculating cache expiry of URL-based resources. 12 | * It is simply configured with a default relative expiry, and each invocation 13 | * of {@link #calculateExpiry(URLConnection)} returns 14 | * {@link System#currentTimeMillis()} incremented with the relative expiry. 15 | * @author Attila Szegedi 16 | * @version $Id: DefaultUrlConnectionExpiryCalculator.java,v 1.3 2011/04/07 20:26:12 hannes%helma.at Exp $ 17 | */ 18 | public class DefaultUrlConnectionExpiryCalculator implements UrlConnectionExpiryCalculator, Serializable { 19 | private static final long serialVersionUID = 1L; 20 | private final long relativeExpiry; 21 | 22 | /** 23 | * Creates a new default expiry calculator with one minute relative expiry. 24 | */ 25 | public DefaultUrlConnectionExpiryCalculator() { 26 | this(60000L); 27 | } 28 | 29 | /** 30 | * Creates a new default expiry calculator with the specified relative 31 | * expiry. 32 | * @param relativeExpiry the fixed relative expiry, in milliseconds. 33 | */ 34 | public DefaultUrlConnectionExpiryCalculator(long relativeExpiry) { 35 | if(relativeExpiry < 0) { 36 | throw new IllegalArgumentException("relativeExpiry < 0"); 37 | } 38 | this.relativeExpiry = relativeExpiry; 39 | } 40 | 41 | @Override 42 | public long calculateExpiry(URLConnection urlConnection) { 43 | return System.currentTimeMillis() + relativeExpiry; 44 | } 45 | } -------------------------------------------------------------------------------- /src/org/mozilla/javascript/commonjs/module/provider/MultiModuleScriptProvider.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.commonjs.module.provider; 6 | 7 | import java.net.URI; 8 | import java.util.LinkedList; 9 | import java.util.List; 10 | 11 | import org.mozilla.javascript.Context; 12 | import org.mozilla.javascript.Scriptable; 13 | import org.mozilla.javascript.commonjs.module.ModuleScript; 14 | import org.mozilla.javascript.commonjs.module.ModuleScriptProvider; 15 | 16 | /** 17 | * A multiplexer for module script providers. 18 | * @author Attila Szegedi 19 | * @version $Id: MultiModuleScriptProvider.java,v 1.4 2011/04/07 20:26:12 hannes%helma.at Exp $ 20 | */ 21 | public class MultiModuleScriptProvider implements ModuleScriptProvider 22 | { 23 | private final ModuleScriptProvider[] providers; 24 | 25 | /** 26 | * Creates a new multiplexing module script provider tht gathers the 27 | * specified providers 28 | * @param providers the providers to multiplex. 29 | */ 30 | public MultiModuleScriptProvider(Iterable providers) { 31 | final List l = new LinkedList(); 32 | for (ModuleScriptProvider provider : providers) { 33 | l.add(provider); 34 | } 35 | this.providers = l.toArray(new ModuleScriptProvider[l.size()]); 36 | } 37 | 38 | @Override 39 | public ModuleScript getModuleScript(Context cx, String moduleId, URI uri, 40 | URI base, Scriptable paths) throws Exception { 41 | for (ModuleScriptProvider provider : providers) { 42 | final ModuleScript script = provider.getModuleScript(cx, moduleId, 43 | uri, base, paths); 44 | if(script != null) { 45 | return script; 46 | } 47 | } 48 | return null; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/commonjs/module/provider/ParsedContentType.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.commonjs.module.provider; 6 | 7 | import java.io.Serializable; 8 | import java.util.StringTokenizer; 9 | 10 | /** 11 | * Breaks a "contentType; charset=encoding" MIME type into content type and 12 | * encoding parts. 13 | * @author Attila Szegedi 14 | * @version $Id: ParsedContentType.java,v 1.3 2011/04/07 20:26:12 hannes%helma.at Exp $ 15 | */ 16 | public final class ParsedContentType implements Serializable { 17 | private static final long serialVersionUID = 1L; 18 | private final String contentType; 19 | private final String encoding; 20 | 21 | /** 22 | * Creates a new parsed content type. 23 | * @param mimeType the full MIME type; typically the value of the 24 | * "Content-Type" header of some MIME-compliant message. Can be null. 25 | */ 26 | public ParsedContentType(String mimeType) { 27 | String contentType = null; 28 | String encoding = null; 29 | if(mimeType != null) { 30 | StringTokenizer tok = new StringTokenizer(mimeType, ";"); 31 | if(tok.hasMoreTokens()) { 32 | contentType = tok.nextToken().trim(); 33 | while(tok.hasMoreTokens()) { 34 | String param = tok.nextToken().trim(); 35 | if(param.startsWith("charset=")) { 36 | encoding = param.substring(8).trim(); 37 | int l = encoding.length(); 38 | if(l > 0) { 39 | if(encoding.charAt(0) == '"') { 40 | encoding = encoding.substring(1); 41 | } 42 | if(encoding.charAt(l - 1) == '"') { 43 | encoding = encoding.substring(0, l - 1); 44 | } 45 | } 46 | break; 47 | } 48 | } 49 | } 50 | } 51 | this.contentType = contentType; 52 | this.encoding = encoding; 53 | } 54 | 55 | /** 56 | * Returns the content type (without charset declaration) of the MIME type. 57 | * @return the content type (without charset declaration) of the MIME type. 58 | * Can be null if the MIME type was null. 59 | */ 60 | public String getContentType() { 61 | return contentType; 62 | } 63 | 64 | /** 65 | * Returns the character encoding of the MIME type. 66 | * @return the character encoding of the MIME type. Can be null when it is 67 | * not specified. 68 | */ 69 | public String getEncoding() { 70 | return encoding; 71 | } 72 | } -------------------------------------------------------------------------------- /src/org/mozilla/javascript/commonjs/module/provider/StrongCachingModuleScriptProvider.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.commonjs.module.provider; 6 | 7 | import java.util.Map; 8 | import java.util.concurrent.ConcurrentHashMap; 9 | 10 | import org.mozilla.javascript.commonjs.module.ModuleScript; 11 | 12 | /** 13 | * A module script provider that uses a module source provider to load modules 14 | * and caches the loaded modules. It strongly references the loaded modules, 15 | * thus a module once loaded will not be eligible for garbage collection before 16 | * the module provider itself becomes eligible. 17 | * @author Attila Szegedi 18 | * @version $Id: StrongCachingModuleScriptProvider.java,v 1.3 2011/04/07 20:26:12 hannes%helma.at Exp $ 19 | */ 20 | public class StrongCachingModuleScriptProvider extends CachingModuleScriptProviderBase { 21 | 22 | private static final long serialVersionUID = 1L; 23 | private final Map modules = new ConcurrentHashMap(16, .75f, getConcurrencyLevel()); 24 | 25 | /** 26 | * Creates a new module provider with the specified module source provider. 27 | * @param moduleSourceProvider provider for modules' source code 28 | */ 29 | public StrongCachingModuleScriptProvider( 30 | ModuleSourceProvider moduleSourceProvider) 31 | { 32 | super(moduleSourceProvider); 33 | } 34 | 35 | @Override 36 | protected CachedModuleScript getLoadedModule(String moduleId) { 37 | return modules.get(moduleId); 38 | } 39 | 40 | @Override 41 | protected void putLoadedModule(String moduleId, ModuleScript moduleScript, 42 | Object validator) { 43 | modules.put(moduleId, new CachedModuleScript(moduleScript, validator)); 44 | } 45 | } -------------------------------------------------------------------------------- /src/org/mozilla/javascript/commonjs/module/provider/UrlConnectionExpiryCalculator.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.commonjs.module.provider; 6 | 7 | import java.net.URLConnection; 8 | 9 | /** 10 | * Implemented by objects that can be used as heuristic strategies for 11 | * calculating the expiry of a cached resource in cases where the server of the 12 | * resource doesn't provide explicit expiry information. 13 | * @author Attila Szegedi 14 | * @version $Id: UrlConnectionExpiryCalculator.java,v 1.3 2011/04/07 20:26:12 hannes%helma.at Exp $ 15 | */ 16 | public interface UrlConnectionExpiryCalculator 17 | { 18 | /** 19 | * Given a URL connection, returns a calculated heuristic expiry time (in 20 | * terms of milliseconds since epoch) for the resource. 21 | * @param urlConnection the URL connection for the resource 22 | * @return the expiry for the resource 23 | */ 24 | public long calculateExpiry(URLConnection urlConnection); 25 | } 26 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/commonjs/module/provider/UrlConnectionSecurityDomainProvider.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.commonjs.module.provider; 6 | 7 | import java.net.URLConnection; 8 | 9 | import org.mozilla.javascript.Context; 10 | 11 | /** 12 | * Interface for URL connection based security domain providers. Used by 13 | * {@link UrlModuleSourceProvider} to create Rhino security domain objects for 14 | * newly loaded scripts (see {@link Context#compileReader(java.io.Reader, 15 | * String, int, Object)}, in {@link Context}) based on the properties obtainable through their URL 16 | * connection. 17 | * @author Attila Szegedi 18 | * @version $Id: UrlConnectionSecurityDomainProvider.java,v 1.3 2011/04/07 20:26:12 hannes%helma.at Exp $ 19 | */ 20 | public interface UrlConnectionSecurityDomainProvider 21 | { 22 | /** 23 | * Create a new security domain object for a script source identified by 24 | * its URL connection. 25 | * @param urlConnection the URL connection of the script source 26 | * @return the security domain object for the script source. Can be null if 27 | * no security domain object can be created, although it is advisable for 28 | * the implementations to be able to create a security domain object for 29 | * any URL connection. 30 | */ 31 | public Object getSecurityDomain(URLConnection urlConnection); 32 | } -------------------------------------------------------------------------------- /src/org/mozilla/javascript/commonjs/module/provider/package.html: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | Provides default implementations for module providers. 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/debug/DebugFrame.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript.debug; 10 | 11 | import org.mozilla.javascript.Context; 12 | import org.mozilla.javascript.Scriptable; 13 | 14 | /** 15 | Interface to implement if the application is interested in receiving debug 16 | information during execution of a particular script or function. 17 | */ 18 | public interface DebugFrame { 19 | 20 | /** 21 | Called when execution is ready to start bytecode interpretation for entered a particular function or script. 22 | 23 | @param cx current Context for this thread 24 | @param activation the activation scope for the function or script. 25 | @param thisObj value of the JavaScript this object 26 | @param args the array of arguments 27 | */ 28 | public void onEnter(Context cx, Scriptable activation, 29 | Scriptable thisObj, Object[] args); 30 | /** 31 | Called when executed code reaches new line in the source. 32 | @param cx current Context for this thread 33 | @param lineNumber current line number in the script source 34 | */ 35 | public void onLineChange(Context cx, int lineNumber); 36 | 37 | /** 38 | Called when thrown exception is handled by the function or script. 39 | @param cx current Context for this thread 40 | @param ex exception object 41 | */ 42 | public void onExceptionThrown(Context cx, Throwable ex); 43 | 44 | /** 45 | Called when the function or script for this frame is about to return. 46 | @param cx current Context for this thread 47 | @param byThrow if true function will leave by throwing exception, otherwise it 48 | will execute normal return 49 | @param resultOrException function result in case of normal return or 50 | exception object if about to throw exception 51 | */ 52 | public void onExit(Context cx, boolean byThrow, Object resultOrException); 53 | 54 | /** 55 | Called when the function or script executes a 'debugger' statement. 56 | @param cx current Context for this thread 57 | */ 58 | public void onDebuggerStatement(Context cx); 59 | } 60 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/debug/DebuggableObject.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript.debug; 10 | 11 | /** 12 | * This interface exposes debugging information from objects. 13 | */ 14 | public interface DebuggableObject { 15 | 16 | /** 17 | * Returns an array of ids for the properties of the object. 18 | * 19 | *

All properties, even those with attribute {DontEnum}, are listed. 20 | * This allows the debugger to display all properties of the object.

21 | * 22 | * @return an array of java.lang.Objects with an entry for every 23 | * listed property. Properties accessed via an integer index will 24 | * have a corresponding 25 | * Integer entry in the returned array. Properties accessed by 26 | * a String will have a String entry in the returned array. 27 | */ 28 | public Object[] getAllIds(); 29 | } 30 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/debug/DebuggableScript.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript.debug; 10 | 11 | /** 12 | * This interface exposes debugging information from executable 13 | * code (either functions or top-level scripts). 14 | */ 15 | public interface DebuggableScript 16 | { 17 | public boolean isTopLevel(); 18 | 19 | /** 20 | * Returns true if this is a function, false if it is a script. 21 | */ 22 | public boolean isFunction(); 23 | 24 | /** 25 | * Get name of the function described by this script. 26 | * Return null or an empty string if this script is not a function. 27 | */ 28 | public String getFunctionName(); 29 | 30 | /** 31 | * Get number of declared parameters in the function. 32 | * Return 0 if this script is not a function. 33 | * 34 | * @see #getParamAndVarCount() 35 | * @see #getParamOrVarName(int index) 36 | */ 37 | public int getParamCount(); 38 | 39 | /** 40 | * Get number of declared parameters and local variables. 41 | * Return number of declared global variables if this script is not a 42 | * function. 43 | * 44 | * @see #getParamCount() 45 | * @see #getParamOrVarName(int index) 46 | */ 47 | public int getParamAndVarCount(); 48 | 49 | /** 50 | * Get name of a declared parameter or local variable. 51 | * index should be less then {@link #getParamAndVarCount()}. 52 | * If index < {@link #getParamCount()}, return 53 | * the name of the corresponding parameter, otherwise return the name 54 | * of variable. 55 | * If this script is not function, return the name of the declared 56 | * global variable. 57 | */ 58 | public String getParamOrVarName(int index); 59 | 60 | /** 61 | * Get the name of the source (usually filename or URL) 62 | * of the script. 63 | */ 64 | public String getSourceName(); 65 | 66 | /** 67 | * Returns true if this script or function were runtime-generated 68 | * from JavaScript using eval function or Function 69 | * or Script constructors. 70 | */ 71 | public boolean isGeneratedScript(); 72 | 73 | /** 74 | * Get array containing the line numbers that 75 | * that can be passed to DebugFrame.onLineChange(). 76 | * Note that line order in the resulting array is arbitrary 77 | */ 78 | public int[] getLineNumbers(); 79 | 80 | public int getFunctionCount(); 81 | 82 | public DebuggableScript getFunction(int index); 83 | 84 | public DebuggableScript getParent(); 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/debug/Debugger.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript.debug; 10 | 11 | import org.mozilla.javascript.Context; 12 | 13 | /** 14 | Interface to implement if the application is interested in receiving debug 15 | information. 16 | */ 17 | public interface Debugger { 18 | 19 | /** 20 | Called when compilation of a particular function or script into internal 21 | bytecode is done. 22 | 23 | @param cx current Context for this thread 24 | @param fnOrScript object describing the function or script 25 | @param source the function or script source 26 | */ 27 | void handleCompilationDone(Context cx, DebuggableScript fnOrScript, 28 | String source); 29 | 30 | /** 31 | Called when execution entered a particular function or script. 32 | 33 | @return implementation of DebugFrame which receives debug information during 34 | the function or script execution or null otherwise 35 | */ 36 | DebugFrame getFrame(Context cx, DebuggableScript fnOrScript); 37 | } 38 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/engine/BindingsObject.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.engine; 6 | 7 | import org.mozilla.javascript.Context; 8 | import org.mozilla.javascript.Scriptable; 9 | import org.mozilla.javascript.ScriptableObject; 10 | import javax.script.Bindings; 11 | 12 | /** 13 | * This class makes the Bindings object into a Scriptable. That way, we can query and modify 14 | * the contents of the Bindings on demand. 15 | */ 16 | public class BindingsObject 17 | extends ScriptableObject { 18 | private final Bindings bindings; 19 | 20 | BindingsObject(Bindings bindings) { 21 | if (bindings == null) { 22 | throw new IllegalArgumentException("Bindings must not be null"); 23 | } 24 | this.bindings = bindings; 25 | } 26 | 27 | @Override 28 | public String getClassName() { 29 | return "BindingsObject"; 30 | } 31 | 32 | @Override 33 | public Object get(String name, Scriptable start) { 34 | if (!bindings.containsKey(name)) { 35 | return Scriptable.NOT_FOUND; 36 | } 37 | return Context.jsToJava(bindings.get(name), Object.class); 38 | } 39 | 40 | @Override 41 | public void put(String name, Scriptable start, Object value) { 42 | bindings.put(name, Context.javaToJS(value, start)); 43 | } 44 | 45 | @Override 46 | public void delete(String name) { 47 | bindings.remove(name); 48 | } 49 | 50 | @Override 51 | public boolean has(String name, Scriptable start) { 52 | return bindings.containsKey(name); 53 | } 54 | 55 | @Override 56 | public Object[] getIds() { 57 | return bindings.keySet().toArray(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/engine/Builtins.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.engine; 6 | 7 | import java.io.IOException; 8 | import java.io.OutputStreamWriter; 9 | import java.io.Writer; 10 | import javax.script.ScriptContext; 11 | import org.mozilla.javascript.Context; 12 | import org.mozilla.javascript.Function; 13 | import org.mozilla.javascript.ScriptRuntime; 14 | import org.mozilla.javascript.Scriptable; 15 | import org.mozilla.javascript.ScriptableObject; 16 | 17 | /** 18 | *

19 | * This class defines the following built-in functions for the RhinoScriptEngine. 20 | *

21 | *
    22 | *
  • print(arg, arg, ...): Write each argument, concatenated to the ScriptEngine's 23 | * "standard output" as a string.
  • 24 | *
25 | */ 26 | public class Builtins { 27 | 28 | static final Object BUILTIN_KEY = new Object(); 29 | 30 | private Writer stdout; 31 | 32 | void register(Context cx, ScriptableObject scope, ScriptContext sc) { 33 | if (sc.getWriter() == null) { 34 | stdout = new OutputStreamWriter(System.out); 35 | } else { 36 | stdout = sc.getWriter(); 37 | } 38 | 39 | scope.defineFunctionProperties(new String[]{"print"}, 40 | Builtins.class, 41 | ScriptableObject.PERMANENT | ScriptableObject.DONTENUM); 42 | } 43 | 44 | public static void print(Context cx, Scriptable thisObj, Object[] args, Function f) 45 | throws IOException { 46 | Builtins self = getSelf(thisObj); 47 | for (Object arg : args) { 48 | self.stdout.write(ScriptRuntime.toString(arg)); 49 | } 50 | self.stdout.write('\n'); 51 | } 52 | 53 | private static Builtins getSelf(Scriptable scope) { 54 | // Since this class is invoked as a set of anonymous functions, "this" 55 | // in JavaScript does not point to "this" in Java. We set a key on the 56 | // top-level scope to address this. 57 | return (Builtins) ScriptableObject.getTopScopeValue(scope, BUILTIN_KEY); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/engine/RhinoCompiledScript.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.engine; 6 | 7 | import javax.script.CompiledScript; 8 | import javax.script.ScriptContext; 9 | import javax.script.ScriptEngine; 10 | import javax.script.ScriptException; 11 | import org.mozilla.javascript.Script; 12 | 13 | public class RhinoCompiledScript 14 | extends CompiledScript { 15 | 16 | private final RhinoScriptEngine engine; 17 | private final Script script; 18 | 19 | RhinoCompiledScript(RhinoScriptEngine engine, Script script) { 20 | this.engine = engine; 21 | this.script = script; 22 | } 23 | 24 | @Override 25 | public Object eval(ScriptContext context) throws ScriptException { 26 | return engine.eval(script, context); 27 | } 28 | 29 | @Override 30 | public ScriptEngine getEngine() { 31 | return engine; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/engine/RhinoInvocationHandler.java: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | package org.mozilla.javascript.engine; 6 | 7 | import java.lang.reflect.InvocationHandler; 8 | import java.lang.reflect.Method; 9 | 10 | public class RhinoInvocationHandler 11 | implements InvocationHandler { 12 | 13 | private final Object thiz; 14 | private final RhinoScriptEngine engine; 15 | 16 | RhinoInvocationHandler(RhinoScriptEngine engine, Object thiz) { 17 | this.engine = engine; 18 | this.thiz = thiz; 19 | } 20 | 21 | @Override 22 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 23 | return engine.invokeMethodRaw(thiz, method.getName(), method.getReturnType(), args); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/regexp/NativeRegExpCallable.java: -------------------------------------------------------------------------------- 1 | package org.mozilla.javascript.regexp; 2 | 3 | import org.mozilla.javascript.Context; 4 | import org.mozilla.javascript.Function; 5 | import org.mozilla.javascript.Scriptable; 6 | 7 | /** 8 | * Legacy implementation of RegExp was callable, this class exists to preserve this functionality 9 | */ 10 | class NativeRegExpCallable extends NativeRegExp implements Function { 11 | 12 | NativeRegExpCallable(Scriptable scope, RECompiled compiled) { 13 | super(scope, compiled); 14 | } 15 | 16 | NativeRegExpCallable() { 17 | super(); 18 | } 19 | 20 | @Override 21 | public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { 22 | return execSub(cx, scope, args, MATCH); 23 | } 24 | 25 | @Override 26 | public Scriptable construct(Context cx, Scriptable scope, Object[] args) { 27 | return (Scriptable) execSub(cx, scope, args, MATCH); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/regexp/NativeRegExpInstantiator.java: -------------------------------------------------------------------------------- 1 | package org.mozilla.javascript.regexp; 2 | 3 | import org.mozilla.javascript.Context; 4 | import org.mozilla.javascript.Scriptable; 5 | 6 | public class NativeRegExpInstantiator { 7 | 8 | private NativeRegExpInstantiator() {} 9 | 10 | static NativeRegExp withLanguageVersion(int languageVersion) { 11 | if (languageVersion < Context.VERSION_ES6) { 12 | return new NativeRegExpCallable(); 13 | } else { 14 | return new NativeRegExp(); 15 | } 16 | } 17 | 18 | static NativeRegExp withLanguageVersionScopeCompiled(int languageVersion, Scriptable scope, RECompiled compiled) { 19 | if (languageVersion < Context.VERSION_ES6) { 20 | return new NativeRegExpCallable(scope, compiled); 21 | } else { 22 | return new NativeRegExp(scope, compiled); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/regexp/SubString.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.regexp; 8 | 9 | /** 10 | * A utility class for lazily instantiated substrings. 11 | */ 12 | public class SubString { 13 | 14 | public SubString() 15 | { 16 | } 17 | 18 | public SubString(String str) 19 | { 20 | this.str = str; 21 | index = 0; 22 | length = str.length(); 23 | } 24 | 25 | public SubString(String source, int start, int len) 26 | { 27 | str = source; 28 | index = start; 29 | length = len; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return str == null 35 | ? "" 36 | : str.substring(index, index + length); 37 | } 38 | 39 | String str; 40 | int index; 41 | int length; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/resources/Messages_en.properties: -------------------------------------------------------------------------------- 1 | # 2 | # English JavaScript messages file. 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | # 8 | # The base messages file is used for all messages, but this file is 9 | # here so that English is selectable as a language when the default 10 | # Locale also has a translation. 11 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/serialize/ScriptableInputStream.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | // API class 8 | 9 | package org.mozilla.javascript.serialize; 10 | 11 | import java.io.IOException; 12 | import java.io.InputStream; 13 | import java.io.ObjectInputStream; 14 | import java.io.ObjectStreamClass; 15 | 16 | import org.mozilla.javascript.Context; 17 | import org.mozilla.javascript.Scriptable; 18 | import org.mozilla.javascript.Undefined; 19 | import org.mozilla.javascript.UniqueTag; 20 | 21 | /** 22 | * Class ScriptableInputStream is used to read in a JavaScript 23 | * object or function previously serialized with a ScriptableOutputStream. 24 | * References to names in the exclusion list 25 | * replaced with references to the top-level scope specified during 26 | * creation of the ScriptableInputStream. 27 | * 28 | * @author Norris Boyd 29 | */ 30 | 31 | public class ScriptableInputStream extends ObjectInputStream { 32 | 33 | /** 34 | * Create a ScriptableInputStream. 35 | * @param in the InputStream to read from. 36 | * @param scope the top-level scope to create the object in. 37 | */ 38 | public ScriptableInputStream(InputStream in, Scriptable scope) 39 | throws IOException 40 | { 41 | super(in); 42 | this.scope = scope; 43 | enableResolveObject(true); 44 | Context cx = Context.getCurrentContext(); 45 | if (cx != null) { 46 | this.classLoader = cx.getApplicationClassLoader(); 47 | } 48 | } 49 | 50 | @Override 51 | protected Class resolveClass(ObjectStreamClass desc) 52 | throws IOException, ClassNotFoundException 53 | { 54 | String name = desc.getName(); 55 | if (classLoader != null) { 56 | try { 57 | return classLoader.loadClass(name); 58 | } catch (ClassNotFoundException ex) { 59 | // fall through to default loading 60 | } 61 | } 62 | return super.resolveClass(desc); 63 | } 64 | 65 | @Override 66 | protected Object resolveObject(Object obj) 67 | throws IOException 68 | { 69 | if (obj instanceof ScriptableOutputStream.PendingLookup) { 70 | String name = ((ScriptableOutputStream.PendingLookup)obj).getName(); 71 | obj = ScriptableOutputStream.lookupQualifiedName(scope, name); 72 | if (obj == Scriptable.NOT_FOUND) { 73 | throw new IOException("Object " + name + " not found upon " + 74 | "deserialization."); 75 | } 76 | }else if (obj instanceof UniqueTag) { 77 | obj = ((UniqueTag)obj).readResolve(); 78 | }else if (obj instanceof Undefined) { 79 | obj = ((Undefined)obj).readResolve(); 80 | } 81 | return obj; 82 | } 83 | 84 | private Scriptable scope; 85 | private ClassLoader classLoader; 86 | } 87 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/typedarrays/Conversions.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.typedarrays; 8 | 9 | import org.mozilla.javascript.ScriptRuntime; 10 | 11 | /** 12 | * Numeric conversions from section 7 of the ECMAScript 6 standard. 13 | */ 14 | 15 | public class Conversions 16 | { 17 | public static int toInt8(Object arg) 18 | { 19 | return (byte)ScriptRuntime.toInt32(arg); 20 | } 21 | 22 | public static int toUint8(Object arg) 23 | { 24 | return ScriptRuntime.toInt32(arg) & 0xff; 25 | } 26 | 27 | public static int toUint8Clamp(Object arg) 28 | { 29 | double d = ScriptRuntime.toNumber(arg); 30 | if (d <= 0.0) { 31 | return 0; 32 | } 33 | if (d >= 255.0) { 34 | return 255; 35 | } 36 | 37 | // Complex rounding behavior -- see 7.1.11 38 | double f = Math.floor(d); 39 | if ((f + 0.5) < d) { 40 | return (int)(f + 1.0); 41 | } 42 | if (d < (f + 0.5)) { 43 | return (int)f; 44 | } 45 | if (((int)f % 2) != 0) { 46 | return (int)f + 1; 47 | } 48 | return (int)f; 49 | } 50 | 51 | public static int toInt16(Object arg) 52 | { 53 | return (short)ScriptRuntime.toInt32(arg); 54 | } 55 | 56 | public static int toUint16(Object arg) 57 | { 58 | return ScriptRuntime.toInt32(arg) & 0xffff; 59 | } 60 | 61 | public static int toInt32(Object arg) 62 | { 63 | return ScriptRuntime.toInt32(arg); 64 | } 65 | 66 | public static long toUint32(Object arg) 67 | { 68 | return ScriptRuntime.toUint32(arg); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/org/mozilla/javascript/typedarrays/NativeTypedArrayIterator.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.typedarrays; 8 | 9 | import java.util.ListIterator; 10 | import java.util.NoSuchElementException; 11 | 12 | public class NativeTypedArrayIterator 13 | implements ListIterator 14 | { 15 | private final NativeTypedArrayView view; 16 | 17 | /** Position represents the position of the NEXT element */ 18 | private int position; 19 | private int lastPosition = -1; 20 | 21 | NativeTypedArrayIterator(NativeTypedArrayView view, int start) 22 | { 23 | this.view = view; 24 | this.position = start; 25 | } 26 | 27 | @Override 28 | public boolean hasNext() 29 | { 30 | return (position < view.length); 31 | } 32 | 33 | @Override 34 | public boolean hasPrevious() 35 | { 36 | return (position > 0); 37 | } 38 | 39 | @Override 40 | public int nextIndex() 41 | { 42 | return position; 43 | } 44 | 45 | @Override 46 | public int previousIndex() 47 | { 48 | return position - 1; 49 | } 50 | 51 | @Override 52 | public T next() 53 | { 54 | if (hasNext()) { 55 | T ret = view.get(position); 56 | lastPosition = position; 57 | position++; 58 | return ret; 59 | } 60 | throw new NoSuchElementException(); 61 | } 62 | 63 | @Override 64 | public T previous() 65 | { 66 | if (hasPrevious()) { 67 | position--; 68 | lastPosition = position; 69 | return view.get(position); 70 | } 71 | throw new NoSuchElementException(); 72 | } 73 | 74 | @Override 75 | public void set(T t) 76 | { 77 | if (lastPosition < 0) { 78 | throw new IllegalStateException(); 79 | } 80 | view.js_set(lastPosition, t); 81 | } 82 | 83 | @Override 84 | public void remove() 85 | { 86 | throw new UnsupportedOperationException(); 87 | } 88 | 89 | @Override 90 | public void add(T t) 91 | { 92 | throw new UnsupportedOperationException(); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /tools/ci/linux/release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | BUILDROOT=${BUILDROOT:-github/rhino} 4 | 5 | (cd $BUILDROOT; ./gradlew clean jar sourceJar javadocJar distZip) 6 | testStatus=$? 7 | exit ${testStatus} 8 | -------------------------------------------------------------------------------- /tools/ci/linux/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | BUILDROOT=${BUILDROOT:-github/rhino} 4 | 5 | (cd $BUILDROOT; git submodule init; git submodule update) 6 | (cd $BUILDROOT; ./gradlew clean check) 7 | testStatus=$? 8 | 9 | for n in ${BUILDROOT}/buildGradle/test-results/*.xml 10 | do 11 | bn=`basename $n .xml` 12 | mv $n ${BUILDROOT}/buildGradle/test-results/${bn}_sponge_log.xml 13 | done 14 | 15 | exit ${testStatus} 16 | 17 | -------------------------------------------------------------------------------- /toolsrc/org/mozilla/javascript/tools/debugger/GuiCallback.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | package org.mozilla.javascript.tools.debugger; 7 | 8 | /** 9 | * Interface for communication between the debugger and its GUI. This 10 | * should be implemented by the GUI. 11 | */ 12 | public interface GuiCallback { 13 | 14 | /** 15 | * Called when the source text of some script has been changed. 16 | */ 17 | void updateSourceText(Dim.SourceInfo sourceInfo); 18 | 19 | /** 20 | * Called when the interrupt loop has been entered. 21 | */ 22 | void enterInterrupt(Dim.StackFrame lastFrame, 23 | String threadTitle, 24 | String alertMessage); 25 | 26 | /** 27 | * Returns whether the current thread is the GUI's event thread. 28 | * This information is required to avoid blocking the event thread 29 | * from the debugger. 30 | */ 31 | boolean isGuiEventThread(); 32 | 33 | /** 34 | * Processes the next GUI event. This manual pumping of GUI events 35 | * is necessary when the GUI event thread itself has been stopped. 36 | */ 37 | void dispatchNextGuiEvent() throws InterruptedException; 38 | } 39 | -------------------------------------------------------------------------------- /toolsrc/org/mozilla/javascript/tools/debugger/ScopeProvider.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | package org.mozilla.javascript.tools.debugger; 7 | 8 | import org.mozilla.javascript.Scriptable; 9 | 10 | /** 11 | * Interface to provide a scope object for script evaluation to the debugger. 12 | */ 13 | public interface ScopeProvider { 14 | 15 | /** 16 | * Returns the scope object to be used for script evaluation. 17 | */ 18 | Scriptable getScope(); 19 | } -------------------------------------------------------------------------------- /toolsrc/org/mozilla/javascript/tools/debugger/SourceProvider.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | package org.mozilla.javascript.tools.debugger; 7 | 8 | import org.mozilla.javascript.debug.DebuggableScript; 9 | 10 | 11 | /** 12 | * Interface to provide a source of scripts to the debugger. 13 | * @version $Id: SourceProvider.java,v 1.1 2009/10/23 12:49:58 szegedia%freemail.hu Exp $ 14 | */ 15 | public interface SourceProvider { 16 | 17 | /** 18 | * Returns the source of the script. 19 | * @param script the script object 20 | * @return the source code of the script, or null if it can not be provided 21 | * (the provider is not expected to decompile the script, so if it doesn't 22 | * have a readily available source text, it is free to return null). 23 | */ 24 | String getSource(DebuggableScript script); 25 | } -------------------------------------------------------------------------------- /toolsrc/org/mozilla/javascript/tools/debugger/test.js: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | 8 | // Test file for Rhino debugger 9 | 10 | 11 | 12 | var x = 12; 13 | 14 | function sleep(millis) 15 | { 16 | java.lang.Thread.sleep(millis); 17 | } 18 | 19 | function thread_loop_body(counter, thread_title) 20 | { 21 | print("["+counter+"] Thread '"+thread_title); 22 | sleep(1000); 23 | } 24 | 25 | function make_thread(thread_title, repeat_count, loop_body) 26 | { 27 | var Thread = java.lang.Thread; 28 | var thread = new Thread(function() { 29 | for (var i = 0; i < repeat_count; ++i) { 30 | loop_body(i, thread_title); 31 | } 32 | print("[DONE] Thread "+thread_title); 33 | }); 34 | return thread; 35 | } 36 | 37 | function make_thread2(thread_title, repeat_count, loop_body, loop_end) 38 | { 39 | var Thread = java.lang.Thread; 40 | var thread = new Thread(function() { 41 | create_gui(); 42 | for (var i = 0; i < repeat_count; ++i) { 43 | loop_body(i, thread_title); 44 | } 45 | loop_end(thread_title); 46 | }); 47 | return thread; 48 | } 49 | 50 | 51 | var loop_body_text = thread_loop_body.toSource(0); 52 | var loop_body; 53 | eval("loop_body = "+loop_body_text); 54 | 55 | var thread1 = make_thread("A", 5, loop_body); 56 | var thread2 = make_thread("B", 1000, loop_body); 57 | var thread3 = make_thread2("C", 2, loop_body, 58 | function loop_end(thread_title) { 59 | print("[DONE] Thread "+thread_title); 60 | // Do somethig to throw exception 61 | Math.xxxx(); 62 | }); 63 | 64 | thread1.start(); 65 | thread2.start(); 66 | thread3.start(); 67 | 68 | thread1.join(); 69 | thread2.join(); 70 | thread3.join(); 71 | 72 | 73 | function create_gui() 74 | { 75 | var swing = Packages.javax.swing; 76 | var awt = Packages.java.awt; 77 | 78 | var frame = new swing.JFrame("SwingApplication"); 79 | var labelPrefix = "Number of button clicks: "; 80 | var numClicks = 0; 81 | var label = new swing.JLabel(labelPrefix + numClicks); 82 | var button = new swing.JButton("Click Me!"); 83 | button.mnemonic = Packages.java.awt.event.KeyEvent.VK_I; 84 | button.addActionListener(function() { 85 | numClicks += 1; 86 | label.setText(labelPrefix + numClicks); 87 | }); 88 | label.setLabelFor(button); 89 | 90 | var pane = new swing.JPanel(); 91 | pane.setLayout(new awt.GridLayout(0, 1)); 92 | pane.add(button); 93 | pane.add(label); 94 | 95 | frame.getContentPane().add(pane, awt.BorderLayout.CENTER); 96 | frame.addWindowListener(function(event, methodName) { 97 | print(event + " "+methodName); 98 | if (methodName == "windowClosing") { 99 | //java.lang.System.exit(0); 100 | } 101 | }); 102 | 103 | //Finish setting up the frame, and show it. 104 | frame.pack(); 105 | frame.setVisible(true); 106 | 107 | } 108 | 109 | -------------------------------------------------------------------------------- /toolsrc/org/mozilla/javascript/tools/shell/QuitAction.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.tools.shell; 8 | 9 | import org.mozilla.javascript.Context; 10 | 11 | /** 12 | * Defines action to perform in response to quit command. 13 | */ 14 | public interface QuitAction 15 | { 16 | public void quit(Context cx, int exitCode); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /toolsrc/org/mozilla/javascript/tools/shell/SecurityProxy.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.tools.shell; 8 | 9 | import org.mozilla.javascript.Context; 10 | import org.mozilla.javascript.Scriptable; 11 | import org.mozilla.javascript.SecurityController; 12 | 13 | public abstract class SecurityProxy extends SecurityController 14 | { 15 | protected abstract void callProcessFileSecure(Context cx, Scriptable scope, 16 | String filename); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /toolsrc/org/mozilla/javascript/tools/shell/ShellLine.java: -------------------------------------------------------------------------------- 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 2 | * 3 | * This Source Code Form is subject to the terms of the Mozilla Public 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 | 7 | package org.mozilla.javascript.tools.shell; 8 | 9 | import java.io.InputStream; 10 | import java.nio.charset.Charset; 11 | 12 | import org.mozilla.javascript.Scriptable; 13 | 14 | /** 15 | * Provides a specialized input stream for consoles to handle line 16 | * editing, history and completion. Relies on the JLine library 17 | * (see http://jline.sourceforge.net). 18 | */ 19 | @Deprecated 20 | public class ShellLine { 21 | @Deprecated 22 | public static InputStream getStream(Scriptable scope) { 23 | ShellConsole console = ShellConsole.getConsole(scope, 24 | Charset.defaultCharset()); 25 | return (console != null ? console.getIn() : null); 26 | } 27 | } 28 | --------------------------------------------------------------------------------