Wrapping JME code in an extension?

6 views
Skip to first unread message

Ulrich Goertz

unread,
Dec 13, 2020, 4:27:49 PM12/13/20
to Numbas Users
Hello, I was wondering whether it is possible to put code for a JME function into an extension and "evaluate" it there so that the extension can expose a callable which applies the JME function.

(I have a collection of JME functions for my linear algebra problems, such as creating a random matrix in row echelon form, applying some random elementary row transformations, etc. In principle they could be rewritten in Javascript, but the JME code is usually quite short and has the advantage that it is easier to plug in different base fields.)

I have seen that an extension can define/expose functions that are then callable from JME, but in all the examples I have seen, the function body was just Javascript. I am missing the piece that would parse the JME code, plug in the arguments and retrieve the result. It seems that something like that has to happen when an exam is imported as a JSON file and the JME functions defined there are turned into callables.

Thanks for any pointers!

Best, Ulrich

Christian Lawson-Perfect

unread,
Dec 16, 2020, 10:38:13 AM12/16/20
to numbas...@googlegroups.com
The code that is used to make up custom functions defined in questions is in Numbas.jme.variables. There's a function Numbas.jme.variables.makeFunction which does what you want. Here's an example extension, adding a function 'versin' defined as 1-cos(x):

Numbas.addExtension('wrap-jme-function',['jme'],function(extension) {
  var scope = extension.scope;

  scope.addFunction(Numbas.jme.variables.makeFunction({
    language: 'jme',
    name: 'versin',
    definition: '1-cos(x)',
    parameters: [
      {name: 'x', type: 'number'}
    ]
  },scope));
});

I hope that helps!

--
You received this message because you are subscribed to the Google Groups "Numbas Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numbas-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/numbas-users/3a211f20-6103-4a59-a872-edd47653d49dn%40googlegroups.com.

Ulrich Goertz

unread,
Dec 16, 2020, 2:28:28 PM12/16/20
to Numbas Users
That's exactly what I was looking for, thanks! Ulrich
Reply all
Reply to author
Forward
0 new messages