I am in the process of ironing out my codebase so it can compile with ADVANCED_OPTIMIZATIONS on google's closure compiler.
After properly setting up the debugging environment required for this task (source map file, chrome, wrapping the compiled js file) i stumbled upon try catch issues. It appears that extensive use of try/catch statements in my codebase has backfired on me.
On almost all methods and functions i use a typical try { } catch(e) {ss.error(e);} statement, where ss.error() is a generic error handler that depending on environment either prints out debug stuff or reports back the exceptions...
In the process of ironing my codebase, when i get an error that i need to fix, what happens is that instead of having Chrome report the offending file and line, it points to the error handler ss.error(). Thus leaving me with no way of backtracing the problem. The ss.error() func however, does print where the problem originated from:
Error! type:TypeError at Db (/jsc/compiled.js:547:246) msg:Cannot call
method 'ka' of undefined source:
After i get these type of errors i have to do two steps:
1. Fiddle with the compiled code at line 547 char 246 and try to figure out which part in my uncompiled code this refers to...
2. After i locate it, remove the try/catch blocks so i can directly and more clearly see what caused the error...
I must say i am not happy with this workflow and need to find an alternative that will both allow me to properly catch exceptions and debug my compiled and uncompiled code while retaining mind sanity =)
I was thinking of somehow using the Line:CharPosition info to query the source map and have the ss.error() function do the mapping to my uncompiled source code...
ideas?
There is a java interface to the SourceMaps as part of the closure compiler. There are also JS implementations in various states of repair. I try to keep links to them up to date here:
http://code.google.com/p/closure-compiler/wiki/SourceMaps
For the Java implementation you simply load the source map using the SourceMapConsumerFactory, the interface is pretty simple.
Related
TL;DR: How can I access variables/functions/names that are defined in ES Modules from the debugger?
More context: I'm a relatively experienced JavaScript programmer, but new to Modules. I've followed the tutorial at MDN here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules. They have a good set of examples here: https://github.com/mdn/js-examples/tree/master/modules
In that collection, say in the "basic-modules" example, (live code here: https://mdn.github.io/js-examples/modules/basic-modules/) there is, for example, a function called random in the file modules/square.js. Suppose I want to execute that function in the debugger, just to try it out, or because it's my code and I want to test/debug it, or I want to demonstrate to another coder what the function does. All the stuff you expect to do in a REPL or debugger. Is there a way to do that? I've tried both the Firefox debugger and the Chrome debugger, with no luck.
Back in the pre-Modules era, that code would be put into the global namespace (making access easy) or it would be locked up in an IIFE (making access impossible) or maybe in some home-made module system (access depends). I am hoping that the new Modules system still allows the debugger access to the names inside modules.
Thanks.
It says in the docs:
Last but not least, let's make this clear — module features are imported into the scope of a single script — they aren't available in the global scope. Therefore, you will only be able to access imported features in the script they are imported into, and you won't be able to access them from the JavaScript console, for example. You'll still get syntax errors shown in the DevTools, but you'll not be able to use some of the debugging techniques you might have expected to use.
To take your example from before, you'll need to invoke that function from a scope where it is visible, i.e where it's been imported:
import { random } from 'path/to/square.js'
debugger; // you should be able to invoke random() from here
I'm learning v8 now, but I have encountered some problems.
How to set a breakpoint a method's start address in memory if I want to debug a method's C++ implementation.
e.g. var a= new Array(0,1); a.indexOf(1) ; I want to set a breakpoint at slice's beginning, or are there other ways to track the assembler code ?
There are a lot of functions will be complied and writed into a file named snapshot.bin. so I can't set a breakpoint at the beginning of these functions.
You need to check the source code and find the implementation of slice. Then set a gdb/lldb break point in that .cc file: byiltins-typedarray.cc
A lot of functions are defined as builtin or runtime functions.
It depends on the kind of function you want to inspect.
You can compile without snapshot to get around snapshot-related debugging difficulties (at the cost of making startup quite a bit slower: several seconds in Debug mode).
You can modify the respective code generator to emit a break instruction at the beginning of the function. For the example of Array.indexOf, that's probably the easiest solution; the CodeStubAssembler instruction is called DebugBreak().
You can break somewhere else using GDB, find your way to the function in question (e.g. via isolate->builtins), and set a breakpoint on the address of its entry. (This requires a bit of V8 knowledge and/or code reading skills, but it's not difficult.)
You can use various --print-*-code flags to print code to stdout (without breaking on it).
WebStorm IDE having some issues with azure-storage intelligent coding does not find methods for that library (but the code works fine when executed).
image of unresolved function or method
Thank you in advance!
Per my experience, some issues for code completion or highlight hint not always mean the code exists errors, especially for Dynamic Language like JavaScript.
I tried to reproduce your issues, and I found some interesting things.
After I writed the code var azureStorage = require('azure-storage'), the code completion for the symbol azureStorage could not display the function suggestions for azure-storage module. WebStorm regard the varable azureStorage as a normal object, see below.
However, the code completion for any undeclared symbol will display all functions of all modules depended on the project after I commented the code var azureStorage = require('azure-storage'), see below. It seems that WebStorm default bind all functions of all modules with undeclared symbols automatically.
Although we can use the code completion in this way, the require code must be required when the code works.
For more details about Auto-completing code, you can refer to https://www.jetbrains.com/webstorm/help/auto-completing-code.html.
For testing purposes, obviously not for production. What is the best way to do this?
Googling I found this tutorial, and of course the project on github.
For starters which files do I need to run:
// removed
and is there an API reference. I see there is a large comment block in jslint.js that seems to server this purpose but was wondering if there is something easier to read.
Because the client has no file access, I was planning on ajaxing the code in to get its contents.
Please never the mind, on why I want to do this on the client.
If you include the JSLint script you will have access to a single global variable, JSLINT. You can invoke it with a string and an optional map of options:
var valid = JSLINT(code, options);
The result will be true or false, depending on whether the code passed the check based on the provided options.
After this call you can inspect the JSLINT.errors property for an array of warnings, if any.
This is precisely what I have done to build JSLint integration into the editor in the articles on http://jslinterrors.com.
Have you looked at http://jshint.com/ ? Source is available here: https://github.com/jshint/jshint/
The browser bundle is available here: http://jshint.com/get/jshint-2.1.10.js and the docs describe how to call it (http://jshint.com/docs/)
I need to modify WebDriverJS for my purposes. The compiled source is giving me a hard time debugging, though. Describing function names and comments would help me out big time! So I was wondering whether it is possible to compile WebDriverJS without minimizing it's content.
The build.desc for the JavaScript compilation is using js_binary which is using Google Closure Compiler. Anyone of you know how to compile it and preserve functionnames and comments? This would rather be a merge of all sources then a compilation.
Thanks to Chads Post in "Potential differences between compiled and uncompiled Javascript" I've taken a deeper look at the flags of closure compiler.
--compilation_level=WHITESPACE_ONLY preserves function and variable names
--formatting=PRETTY_PRINT doesn't remove linebreaks
--formatting=PRINT_INPUT_DELIMETER gives me a better overview in which file to search for the source
Unfortunately I still couldn't figure out how to save the comments, but thats just a small problem since I can look them up in the source code.
Update:
Seems like the compilation_level doesn't remove the goog.required-calls. I've got to remove them somehow, because the script doesn't work with them.
Update 2:
I've removed all goog.require($mod) and goog.provide($mod) calls and defined Objects where needed (typically to find right after the // Input $int comments). It's working now.