Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9207293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:21:23+00:00 2026-06-18T00:21:23+00:00

I’m calling a @JSFunction annotated method of a ScriptableObject The JavaScript file Target =

  • 0

I’m calling a @JSFunction annotated method of a ScriptableObject

The JavaScript file

Target = Packages.com.acme.rhino.Target;

function evaluate() {
    var t = Target();
    t.addModifier("foobar", 1);
    return t;
}

The Java File

public class Target extends ScriptableObject {
    private static final long serialVersionUID = 1L;
    public List<Modifier> modifiers = new LinkedList<>();

    @JSConstructor
    public Target() {
    }

    @JSFunction
    public void addModifier(final String message, final int value) {
        modifiers.add(new Modifier(message, value));
    }

    public int getValue() {
        int sum = 0;
        for (final Modifier modifier : modifiers) {
            sum += modifier.getValue();
        }
        return sum;
    }

    @Override
    public String getClassName() {
        return "Target";
    }
}

But I get

org.mozilla.javascript.EcmaError: TypeError: Cannot find default value for object.
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)
    at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3693)
    at org.mozilla.javascript.ScriptRuntime.typeError1(ScriptRuntime.java:3705)
    at org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:976    )
    at org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:895    )
    at org.mozilla.javascript.ScriptRuntime.toString(ScriptRuntime.java:761)
    at org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3774)
    at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.    java:2269)
    at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.    java:2251)
    at org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:83)
    at org.mozilla.javascript.gen.script_5._c_evaluate_1(script:6)
    at org.mozilla.javascript.gen.script_5.call(script)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
    at org.mozilla.javascript.gen.script_5.call(script)

and don’t know where to go from there. When I don’t call addModifier method the given code works, and given the error notFunctionError down in the stack trace I think that Rhino doesn’t interpret the given method as a JavaScript Function.

  • OSX 10.8.2
  • Java 7
  • Rhino 1.7R4

Complete Maven project that reproduces the error can be found here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:21:24+00:00Added an answer on June 18, 2026 at 12:21 am

    tl;dr see these two alternatives.

    The problem with the approach above is that Target.prototype is not properly set up in the script scope. See the static ScriptableObject.defineClass() method for details on how to properly define prototypes in a script scope.

    You have a couple alternatives for providing the Target constructor to your scripts. The first alternative would be to always define the Target constructor for all scripts. This works well if you know ahead of time that you want Target to be globally available. This basically comes down to the following:

    final Context context = Context.enter();
    try {
      final ScriptableObject scope = context.initStandardObjects();
      ScriptableObject.defineClass(scope, Target.class, false, true);
      context.evaluateString(scope, script, "script", 1, null);
      // etc.
    } finally {
      Context.exit();
    }
    

    If instead you want the script author to decide which constructors are necessary, the second alternative is to provide the defineClass function to scripts. With this function, script authors can “import” any scriptable objects on their class path (which may be more than you want to allow). To provide the defineClass functions to scripts, do the following after entering the context:

    final Context context = Context.enter();
    try {
      final ScriptableObject scope = context.initStandardObjects();
      scope.defineFunctionProperties(
              new String[] {"defineClass"},
              Global.class,
              ScriptableObject.DONTENUM);
    
      context.evaluateString(scope, script, "script", 1, null);
      // etc.
    } finally {
      Context.exit();
    }
    

    And then, the JavaScript author makes use of the Target constructor with the following:

    defineClass("com.acme.rhino.Target");
    // whatever `getClassName()` returns is now available
    var target = new Target();
    

    In both of the above branches, I’ve made a couple other changes that set you up better if you add more to the Target constructor. The zero argument constructor doesn’t need the @JSConstructor annotation. If you later want to have a constructor that accepts arguments, this zero argument constructor will be used as the prototype constructor, and you can use the @JSConstructor annotation on a method that will be used to initialize your object. Depending on how you author this constructor method, it will become important to use the new keyword in your JavaScript.

    In short, the Packages.com.acme... syntax is not useful for getting access to ScriptableObject constructors from scripts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.