Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow @:native("") with empty string #2925

Closed
eduardo-costa opened this issue Apr 27, 2014 · 4 comments
Closed

Allow @:native("") with empty string #2925

eduardo-costa opened this issue Apr 27, 2014 · 4 comments

Comments

@eduardo-costa
Copy link

Using "native" give us freedom to do stuff like:
@:native("(require('mongodb').MongoClient)")
The resulting code will be " x = new (require('mongodb').MongoClient)(...)"
So far so good, but:
There are some global functions (like 'require' in nodejs) that do not have an object to give access to it.

It would be interesting to create a "context class" with native("") that will compile its attributes "free" of classes scope.

Something like this:

@:native("")
extern class NodeJS
{
@:native("require")
static function require(s:String):Dynamic;
}

Currently Haxe generates this JS:

.hx [ x = NodeJS.require("module"); ]
.js [ x = .require("module") ]

The "." spoils the fun. I think that the native("") could be a special case without the "."

This way things like this:

static public function require(p_lib : String):Dynamic { return untyped js("require(p_lib)"); }

Would not be necessary.

@ncannasse
Copy link
Member

This would not work, because "require" might be a user local variable, so that would easily break.
One other option is to have static inline function require(s) { return untyped __js__('require')(s); } but with the same issue

@eduardo-costa
Copy link
Author

I understand.
But how to cover this case of global functions, without resorting to wrapping the function with the same definition.
Because in nodejs if the user uses a local variable called 'require' he would fall in the same error anyway.

@frabbit
Copy link
Member

frabbit commented Apr 28, 2014

There is currently no good solution, see #1492

@eduardo-costa
Copy link
Author

Sincerely adapting the @:native("") to simply hiding the '.' would solve the problem of global functions.
Local variables conflicting with global stuff is normal.. reserved words will always cause these issues anyway.
But no problem.. this isn't really an urgent issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants