JUCE javascript Throw custom error and get line number?


Here’s the code that hooks my C++ to my javascript engine:

static var name(const var::NativeFunctionArgs & args)
	if (auto obj = getObjectWithArgumentCount<SaltMidiObject>(args, 1))
		try { return g_midi.name(args.arguments[0]); }
		catch (std::exception e)
			obj->owner.log(format_error("Midi.name", args, e.what()));
	return var::undefined();

I was hoping there might be some kind of function like int line_of_error = JavascriptEngine::getCurrentLine(); then I could improve my “format_error” function by adding the line number to the message so that the user would know where I threw an error.


No… not the way the parser is implemented. When the JS is actually running, all the line number information has long ago been thrown away, and it’s just an abstract syntax tree of expressions being evaluated. To add source location information to all those nodes and then implement stack generation from it would be a major addition.


ok, just making sure I’m not missing something. My error currently displays the function name and all arguments’ values for the given function, so that is decent for debugging purposes already.


how is the line number retreived upon time-out? I can get line number of my custom error by using Visual Studio debugger, and just waiting until JUCE Javascript times out before continuing. Then I’ll get the line number via the time out error message. Not trying to be pushy, but… just curious…


Oh, ok… Yes, the nodes do contain a location… I guess the caller of the native method could provide the source location, but to use that in the native method would involve passing it in as an argument, which could be a bit of an overhead.