What determines if a JS 6 script is a module? - module

Normally all global variables and functions in a JS script in a browser are attributes to Window, if I'm right.
With modules that shouldn't be the case. What determines that global vars and funcs do not become attributes to Window? I haven't found a module keyword.
I've been reading quite a lot about this, but it seems to be so self-evident that I could not find an explanation.

Related

Is it possible to prevent testcafe/hammerhead scripts injection in specific iframe?

everyone.
What I want to do - to create an empty/pristine iframe on testcafe page.
Problem:
Testcafe/hammerhead injects custom script, that overrides prototypes and creates many global variables. I need to prevent it somehow. So I need to create iframe on testcafe page, but without all this custom injected stuff.
Is there any API method or "hack" for this?
I use the latest testcafe version.
TestCafe cannot operate with an iframe without embedding service scripts.
I agree that making a global variable is not the best practice in JavaScript.
However, TestCafe's global variable is marked as non-enumerable (see here). It means that the client's script cannot find this global variable because it cannot access it without knowing this variable's name.
In my opinion, the prototype overriding is not a big problem because a lot of JavaScript frameworks (Angular, JQuery, etc.) do the same.

How can I create a single Clojure source file which can be safely used as a script and a library without AOT compilation?

I’ve spent some time researching this and though I’ve found some relevant info,
Here’s what I’ve found:
SO question: “What is the clojure equivalent of the Python idiom if __name__ == '__main__'?”
Some techniques at RosettaCode
A few discussions in the Cojure Google Group — most from 2009
but none of them have answered the question satisfactorily.
My Clojure source code file defines a namespace and a bunch of functions. There’s also a function which I want to be invoked when the source file is run as a script, but never when it’s imported as a library.
So: now that it’s 2012, is there a way to do this yet, without AOT compilation? If so, please enlighten me!
I'm assuming by run as a script you mean via clojure.main as follows:
java -cp clojure.jar clojure.main /path/to/myscript.clj
If so then there is a simple technique: put all the library functions in a separate namespace like mylibrary.clj. Then myscript.clj can use/require this library, as can your other code. But the specific functions in myscript.clj will only get called when it is run as a script.
As a bonus, this also gives you a good project structure, as you don't want script-specific code mixed in with your general library functions.
EDIT:
I don't think there is a robust within Clojure itself way to determine whether a single file was launched as a script or loaded as a library - from Clojure's perspective, there is no difference between the two (it all gets loaded in the same way via Compiler.load(...) in the Clojure source for anyone interested).
Options if you really want to detect the manner of the launch:
Write a main class in Java which sets a static flag then launched the Clojure script. You can easily test this flag from Clojure.
Use AOT compilation to implement a Clojure main class which sets a flag
Use *command-line-args* to indicate script usage. You'll need to pass an extra parameter like "script" on the command line.
Use a platform-specific method to determine the command line (e.g. from the environment variables in Windows)
Use the --eval option in the clojure.main command line to load your clj file and launch a specific function that represents your script. This function can then set a script-specific flag if needed
Use one of the methods for detecting the Java main class at runtime
I’ve come up with an approach which, while deeply flawed, seems to work.
I identify which namespaces are known when my program is running as a script. Then I can compare that number to the number of namespaces known at runtime. The idea is that if the file is being used as a lib, there should be at least one more namespace present than in the script case.
Of course, this is extremely hacky and brittle, but it does seem to work:
(defn running-as-script
"This is hacky and brittle but it seems to work. I’d love a better
way to do this; see http://stackoverflow.com/q/9027265"
[]
(let
[known-namespaces
#{"clojure.set"
"user"
"clojure.main"
"clj-time.format"
"clojure.core"
"rollup"
"clj-time.core"
"clojure.java.io"
"clojure.string"
"clojure.core.protocols"}]
(= (count (all-ns)) (count known-namespaces))))
This might be helpful: the github project lein-oneoff describes itself as "dependency management for one-off, single-file clojure programs."
This lets you define everything in one file, but you do need the oneoff plugin installed in order to run it from the command line.

OCaml - testing functions not included in the signature

I'm writing tests for an OCaml module. Some of the functions in the module are not meant to be publicly visible, and so they're not included in the signature (.mli file).
I can't call these functions from my tests, because they're not visible outside of the module. So I'm having a hard time testing them. Is there a good way to get around this? For example, a way to tell ocamlc not to read the signature from the .mli file when it's compiling tests?
Some ideas:
Actually export the test functions, but use ocamldoc's stop comment (**/**) feature to avoid displaying the exports in the documentation.
Put all of your tests entirely in another module. However, this is difficult if you have abstract types because your tests may very well need access to the internal implementation.
Create a submodule Test, where all your tests go. That way it is clear what functions are just for testing. Possibly combine this with the (**/**) feature to also hide the sub-module from documentation.
I've heard that people sometimes separate their .mli files from their .ml files (in a different directory) so that they can compile with or without them (by telling ocamlc to look in the separate directory or not). I just tried a few experiments with this. I think it can be made to work, but it seems a little bit error prone to me. Maybe you could put the tests of the internal functions into the module. Exporting the test functions might not violate the modularity too badly. (Though of course it clutters up the module.)

What's the best method for traversing long scripts?

When your script gets to be thousands of lines long, finding a particular function or variable declaration gets to be a real pain. Are there any methods you can use to avoid this?
It really depends on the language and the editor you use.
If the language supports importing from external files, as most of them do, you should refactor your script into smaller modules and import/include them into your main script.
Also, most editors have some means of searching within a file and some, such as 'TextMate' (on Mac) or 'e', its Windows clone, provide a special view displaying all the symbols within the source which you can click on to immediately reorient the editor to the chosen target.
You split your code into separate files/modules/etc., generally organized by similar functionality, and require them in your main script.

Tool to Identifying Global Variables in VB.NET

Is there an automated tool for VB.Net that will identify all global variables in a project?
Short of that, is there any scripts that can be used that will facilitate a manual review of global variables?
There seems to be tools for C/C++, but not for VB.Net:
Tools to find global/static variables in C codebase
Is there a tool to list global variables used and output by a C function?
EDIT:
My current approach uses the following VS REGEX searches:
For finding global variables:
~(Private:b+)Shared:b+:i:b+As:b+
For finding global properties:
~(Private:b+)Shared:b+(~(ReadOnly:b+):i:b+)#Property:b+:i([(][)]|[]):b+As:b+
fxCop might help, but you might need to write a plugin for that particular function.
Also, ndepend (Using the CQL "code query langauge") I'm pretty sure will give you a report or globals (and a whole lot more).
What are you calling a "global variable"? a variable defined as Shared (private or otherwise) isn't 'global' at all.
Really, the only 'global' variables left in vb.net are declared as public in a module