Giter Site home page Giter Site logo

Comments (2)

NeilFraser avatar NeilFraser commented on August 17, 2024 1

Good questions.

First, in order to convert native JavaScript objects into interpreter objects, we're going to need access to the interpreter. One way to do this is to move the API function definitions inside the init function, so they have access to the interpreter variable.

	const init = function (interpreter, globalObject) {
		const funcs = {
			sq: (x) => x * x,
			sq2: (args) => {
				console.log(`sq2`, { args: args });
				return args.x * args.x;
			},
			times: (a1, a2) => {
				console.log(`times`, { a1, a2 });
				return a1 * a2;
			}
		};
		const scope = interpreter.nativeToPseudo({});
		interpreter.setProperty(globalObject, 'scope', scope);
		for (const key in funcs) {
			if (typeof funcs[key] === 'function') {
				interpreter.setProperty(
					scope,
					key,
					interpreter.createNativeFunction((args) => funcs[key](args))
				);
			}
		}
		interpreter.setProperty(
			globalObject,
			'setOutput',
			interpreter.createNativeFunction(outputWrapper)
		);
	};

With that done, now we can convert the JS-Interpreter object args into a native JavaScript object:

			sq2: (args) => {
				args = interpreter.pseudoToNative(args);
				console.log(`sq2`, { args: args });
				return args.x * args.x;
			},

And finally, we need to simplify your loop for defining these functions, so that it doesn't force a definition with a single argument:

					interpreter.createNativeFunction(funcs[key])

Hope this helps!

from js-interpreter.

tsdexter avatar tsdexter commented on August 17, 2024

Thanks a lot for the quick response. I will try it out

from js-interpreter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.