Giter Club home page Giter Club logo

Comments (2)

NeilFraser avatar NeilFraser commented on July 28, 2024

My understanding is that you are expecting a user to enter some code (hopefully a function) into your editor, and you want to know the function name that has been defined. For example, the user enters this code:

function foo() {
  alert('hello')
}

And you want to know that the function is called foo.

The best way I can think of is to compare the global variables before and after the code is injected.

// Create an empty interpreter with no code (and an initFunc for any custom API functions).
var myInterpreter = new Interpreter('', initFunc);
// Record the global names in an empty JavaScript environment.
var oldGlobals = Object.getOwnPropertyNames(myInterpreter.getGlobalScope().object.properties);
// Load the user's code.
myInterpreter.appendCode(code);
// Record the global names after the user's code has been added.
var newGlobals = Object.getOwnPropertyNames(myInterpreter.getGlobalScope().object.properties);
// Compute the difference to determine what's new.
var userGlobals = newGlobals.filter(n => !oldGlobals.includes(n));

This produces an array of all globals which the user's code created. Hopefully that will be the function they defined. But it could also be a global variable. You can use this code to find out if a particular value is a function or not:

myInterpreter.getValueFromScope('foo').class === 'Function'

from js-interpreter.

cpcallen avatar cpcallen commented on July 28, 2024

I think a better approach would be to parse the user-provided code using Acorn and extract the function names from the generated AST. This avoids a whole host of potential pitfalls and additionally obviates the need to use JS Interpreter for this task.

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.