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

Custom js generator, dce and typedefs #9252

Open
benmerckx opened this issue Mar 17, 2020 · 4 comments
Open

Custom js generator, dce and typedefs #9252

benmerckx opened this issue Mar 17, 2020 · 4 comments
Labels
platform-javascript Everything related to JS / JavaScript
Milestone

Comments

@benmerckx
Copy link
Contributor

When enabling DCE, a custom js generator receives only those concrete types that need to be generated. However it also receives every typedef included in the class path (even in unused modules). I'm generating Typescript definitions - and some of these typedefs reference concrete types that are not part of the generation (making Typescript upset). So I'm looking to either have unused typedefs not pass through to the generate call or for a way to check during generation if a typedef is in use?

#if macro
import haxe.macro.Compiler;
import haxe.macro.JSGenApi;
#end

typedef Unused = {a: Int}

class Main {
  #if macro
  static function generate(api: JSGenApi) {
    for (type in api.types)
      switch type {
        case TType(t, _): trace(t);
        default:
      }
  }
  public static function use() {
    Compiler.setCustomJSGenerator(generate);
  }
  #end
  static function main() {
    trace('hello');
  }
}
$ haxe --macro Main.use() --main Main --js out.js --dce full
Main.hx:13: Unused
Main.hx:13: Iterator
Main.hx:13: Iterable
Main.hx:13: KeyValueIterator
Main.hx:13: KeyValueIterable
Main.hx:13: js.lib.ObjectPrototype
Main.hx:13: js.lib.ObjectPropertyDescriptor
@benmerckx benmerckx changed the title Custom js compiler, dce and typedefs Custom js generator, dce and typedefs Mar 17, 2020
@RealyUniqueName
Copy link
Member

I guess the issue is that onGenerate macros get executed before DCE (check the line numbers):

List.iter (fun f -> f()) (List.rev com.callbacks#get_before_save); (* macros onGenerate etc. *)

Dce.run com main dce_mode;

@RealyUniqueName
Copy link
Member

@Simn Do you know why do we have it before dce?

@Simn
Copy link
Member

Simn commented Mar 18, 2020

I know absolutely nothing about custom JS generators.

@benmerckx
Copy link
Contributor Author

I guess the issue is that onGenerate macros get executed before DCE (check the line numbers):

The js generation is actually called at a later stage than onGenerate:

#if macro
import haxe.macro.Compiler;
import haxe.macro.JSGenApi;
import haxe.macro.Context;
#end

typedef Unused = UnusedConcrete;

class UnusedConcrete {}

class Main {
  #if macro
  static function generate(api: JSGenApi) {
    trace('js generate');
    for (type in api.types)
      switch type {
        case TInst(_.get() => {name: name}, _) | TType(_.get() => {name: name}, _):
          if (name.substr(0, 2) == 'Un')
            trace(name);
        default:
      }
  }
  public static function use() {
    Context.onGenerate(_ -> trace('onGenerate'));
    Compiler.setCustomJSGenerator(generate);
  }
  #end
  static function main() {
    trace('hello');
  }
}
$ haxe --macro Main.use() --main Main --js out.js
Main.hx:24: onGenerate
Main.hx:14: js generate
Main.hx:19: Unused
Main.hx:19: UnusedConcrete

With dce the unused class UnusedConcrete is not passed to the generate call, but the typedef referencing it is:

$ haxe --macro Main.use() --main Main --js out.js --dce full
Main.hx:24: onGenerate
Main.hx:14: js generate
Main.hx:19: Unused

@RealyUniqueName RealyUniqueName added this to the Bugs milestone Mar 30, 2020
@RealyUniqueName RealyUniqueName added the platform-javascript Everything related to JS / JavaScript label Mar 30, 2020
benmerckx added a commit to benmerckx/genes that referenced this issue May 26, 2021
…don't they're indistinguishable from import x in y. We just have to wait for HaxeFoundation/haxe#9252
@Simn Simn modified the milestones: Bugs, Later Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform-javascript Everything related to JS / JavaScript
Projects
None yet
Development

No branches or pull requests

3 participants