Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

Do not generate API functions for private or opaque custom types

author
GearsDatapacks
committer
Louis Pilfold
date (Sep 19, 2025, 11:58 AM +0100) commit f9346e2e parent 2654a2e9 change-id xwtswyqq
+26 -201
+26 -24
compiler-core/src/javascript.rs
··· 354 354 355 355 self.tracker.custom_type_used = true; 356 356 357 + let constructor_publicity = if opaque || publicity.is_private() { 358 + Publicity::Private 359 + } else { 360 + Publicity::Public 361 + }; 362 + 357 363 let mut definitions = constructors 358 364 .iter() 359 - .map(|constructor| self.record_definition(constructor, name, publicity, opaque)) 365 + .map(|constructor| self.record_definition(constructor, name, constructor_publicity)) 360 366 .collect_vec(); 361 367 362 368 // Generate getters for fields shared between variants ··· 365 371 // Don't bother generating shared getters when there's only one variant, 366 372 // since the specific accessors can always be uses instead. 367 373 && constructors.len() != 1 374 + // Only generate accessors for the API if the constructors are public 375 + && constructor_publicity.is_public() 368 376 { 369 377 let function_head = if opaque || publicity.is_private() { 370 378 "function " ··· 386 394 constructor: &'a TypedRecordConstructor, 387 395 type_name: &'a str, 388 396 publicity: Publicity, 389 - opaque: bool, 390 397 ) -> Document<'a> { 391 - let class_definition = self.record_class_definition(constructor, publicity, opaque); 392 - let function_head = if opaque || publicity.is_private() { 393 - "function " 394 - } else { 395 - "export function " 396 - }; 397 - let constructor_definition = 398 - self.record_constructor_definition(constructor, type_name, function_head); 399 - let variant_check_definition = 400 - self.record_check_definition(constructor, type_name, function_head); 401 - let fields_definition = 402 - self.record_fields_definition(constructor, type_name, function_head); 398 + let class_definition = self.record_class_definition(constructor, publicity); 399 + 400 + // If the custom type is private or opaque, we don't need to generate API 401 + // functions for it. 402 + if publicity.is_private() { 403 + return class_definition; 404 + } 405 + 406 + let constructor_definition = self.record_constructor_definition(constructor, type_name); 407 + let variant_check_definition = self.record_check_definition(constructor, type_name); 408 + let fields_definition = self.record_fields_definition(constructor, type_name); 403 409 404 410 docvec![ 405 411 class_definition, ··· 415 421 &self, 416 422 constructor: &'a TypedRecordConstructor, 417 423 type_name: &'a str, 418 - function_head: &'a str, 419 424 ) -> Document<'a> { 420 425 let mut arguments = Vec::new(); 421 426 ··· 437 442 ]; 438 443 439 444 docvec![ 440 - function_head, 445 + "export function ", 441 446 type_name, 442 447 "$", 443 448 constructor.name.as_str(), ··· 454 459 &self, 455 460 constructor: &'a TypedRecordConstructor, 456 461 type_name: &'a str, 457 - function_head: &'a str, 458 462 ) -> Document<'a> { 459 463 let construction = docvec![ 460 464 line(), ··· 464 468 ]; 465 469 466 470 docvec![ 467 - function_head, 471 + "export function ", 468 472 type_name, 469 473 "$is", 470 474 constructor.name.as_str(), ··· 479 483 &self, 480 484 constructor: &'a TypedRecordConstructor, 481 485 type_name: &'a str, 482 - function_head: &'a str, 483 486 ) -> Document<'a> { 484 487 let mut functions = Vec::new(); 485 488 ··· 505 508 506 509 functions.push(docvec![ 507 510 line(), 508 - function_head, 511 + "export function ", 509 512 function_name, 510 513 "(value) {", 511 514 contents.nest(INDENT), ··· 548 551 &self, 549 552 constructor: &'a TypedRecordConstructor, 550 553 publicity: Publicity, 551 - opaque: bool, 552 554 ) -> Document<'a> { 553 555 fn parameter((i, arg): (usize, &TypedRecordConstructorArg)) -> Document<'_> { 554 556 arg.label ··· 564 566 nil() 565 567 }; 566 568 567 - let head = if publicity.is_private() || opaque { 568 - "class " 569 - } else { 569 + let head = if publicity.is_public() { 570 570 "export class " 571 + } else { 572 + "class " 571 573 }; 572 574 let head = docvec![head, &constructor.name, " extends $CustomType {"]; 573 575
-18
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assignments__assert_that_always_fails.snap
··· 26 26 this[0] = $0; 27 27 } 28 28 } 29 - function Wibble$Wibble($0) { 30 - return new Wibble($0); 31 - } 32 - function Wibble$isWibble(value) { 33 - return value instanceof Wibble; 34 - } 35 - function Wibble$Wibble$0(value) { 36 - return value[0]; 37 - } 38 29 39 30 class Wobble extends $CustomType { 40 31 constructor($0) { 41 32 super(); 42 33 this[0] = $0; 43 34 } 44 - } 45 - function Wibble$Wobble($0) { 46 - return new Wobble($0); 47 - } 48 - function Wibble$isWobble(value) { 49 - return value instanceof Wobble; 50 - } 51 - function Wibble$Wobble$0(value) { 52 - return value[0]; 53 35 } 54 36 55 37 export function go() {
-9
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assignments__assert_that_always_succeeds.snap
··· 23 23 this[0] = $0; 24 24 } 25 25 } 26 - function Wibble$Wibble($0) { 27 - return new Wibble($0); 28 - } 29 - function Wibble$isWibble(value) { 30 - return value instanceof Wibble; 31 - } 32 - function Wibble$Wibble$0(value) { 33 - return value[0]; 34 - } 35 26 36 27 export function go() { 37 28 let $ = new Wibble(1);
-27
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assignments__assert_with_multiple_variants.snap
··· 27 27 this[0] = $0; 28 28 } 29 29 } 30 - function Wibble$Wibble($0) { 31 - return new Wibble($0); 32 - } 33 - function Wibble$isWibble(value) { 34 - return value instanceof Wibble; 35 - } 36 - function Wibble$Wibble$0(value) { 37 - return value[0]; 38 - } 39 30 40 31 class Wobble extends $CustomType { 41 32 constructor($0) { ··· 43 34 this[0] = $0; 44 35 } 45 36 } 46 - function Wibble$Wobble($0) { 47 - return new Wobble($0); 48 - } 49 - function Wibble$isWobble(value) { 50 - return value instanceof Wobble; 51 - } 52 - function Wibble$Wobble$0(value) { 53 - return value[0]; 54 - } 55 37 56 38 class Woo extends $CustomType { 57 39 constructor($0) { 58 40 super(); 59 41 this[0] = $0; 60 42 } 61 - } 62 - function Wibble$Woo($0) { 63 - return new Woo($0); 64 - } 65 - function Wibble$isWoo(value) { 66 - return value instanceof Woo; 67 - } 68 - function Wibble$Woo$0(value) { 69 - return value[0]; 70 43 } 71 44 72 45 export function go() {
-18
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assignments__catch_all_assert.snap
··· 24 24 this[0] = $0; 25 25 } 26 26 } 27 - function Wibble$Wibble($0) { 28 - return new Wibble($0); 29 - } 30 - function Wibble$isWibble(value) { 31 - return value instanceof Wibble; 32 - } 33 - function Wibble$Wibble$0(value) { 34 - return value[0]; 35 - } 36 27 37 28 class Wobble extends $CustomType { 38 29 constructor($0) { 39 30 super(); 40 31 this[0] = $0; 41 32 } 42 - } 43 - function Wibble$Wobble($0) { 44 - return new Wobble($0); 45 - } 46 - function Wibble$isWobble(value) { 47 - return value instanceof Wobble; 48 - } 49 - function Wibble$Wobble$0(value) { 50 - return value[0]; 51 33 } 52 34 53 35 export function go() {
-9
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assignments__let_assert_nested_string_prefix.snap
··· 25 25 this.wibble = wibble; 26 26 } 27 27 } 28 - function Wibble$Wibble(wibble) { 29 - return new Wibble(wibble); 30 - } 31 - function Wibble$isWibble(value) { 32 - return value instanceof Wibble; 33 - } 34 - function Wibble$Wibble$wibble(value) { 35 - return value.wibble; 36 - } 37 28 38 29 export function main() { 39 30 let $ = new Wibble("wibble");
-27
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__assignments__use_discard_assignment.snap
··· 27 27 this[0] = $0; 28 28 } 29 29 } 30 - function Wibble$Wibble($0) { 31 - return new Wibble($0); 32 - } 33 - function Wibble$isWibble(value) { 34 - return value instanceof Wibble; 35 - } 36 - function Wibble$Wibble$0(value) { 37 - return value[0]; 38 - } 39 30 40 31 class Wobble extends $CustomType { 41 32 constructor($0) { ··· 43 34 this[0] = $0; 44 35 } 45 36 } 46 - function Wibble$Wobble($0) { 47 - return new Wobble($0); 48 - } 49 - function Wibble$isWobble(value) { 50 - return value instanceof Wobble; 51 - } 52 - function Wibble$Wobble$0(value) { 53 - return value[0]; 54 - } 55 37 56 38 class Woo extends $CustomType { 57 39 constructor($0) { 58 40 super(); 59 41 this[0] = $0; 60 42 } 61 - } 62 - function Wibble$Woo($0) { 63 - return new Woo($0); 64 - } 65 - function Wibble$isWoo(value) { 66 - return value instanceof Woo; 67 - } 68 - function Wibble$Woo$0(value) { 69 - return value[0]; 70 43 } 71 44 72 45 function fun(f) {
-27
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__case__deeply_nested_string_prefix_assignment.snap
··· 34 34 this[0] = $0; 35 35 } 36 36 } 37 - function Wibble$Wibble($0) { 38 - return new Wibble($0); 39 - } 40 - function Wibble$isWibble(value) { 41 - return value instanceof Wibble; 42 - } 43 - function Wibble$Wibble$0(value) { 44 - return value[0]; 45 - } 46 37 47 38 class Wobble extends $CustomType { 48 39 constructor(wabble) { ··· 50 41 this.wabble = wabble; 51 42 } 52 43 } 53 - function Wobble$Wobble(wabble) { 54 - return new Wobble(wabble); 55 - } 56 - function Wobble$isWobble(value) { 57 - return value instanceof Wobble; 58 - } 59 - function Wobble$Wobble$wabble(value) { 60 - return value.wabble; 61 - } 62 44 63 45 class Wabble extends $CustomType { 64 46 constructor(tuple) { 65 47 super(); 66 48 this.tuple = tuple; 67 49 } 68 - } 69 - function Wabble$Wabble(tuple) { 70 - return new Wabble(tuple); 71 - } 72 - function Wabble$isWabble(value) { 73 - return value instanceof Wabble; 74 - } 75 - function Wabble$Wabble$tuple(value) { 76 - return value.tuple; 77 50 } 78 51 79 52 export function main() {
-9
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__case__nested_string_prefix_assignment.snap
··· 28 28 this.wobble = wobble; 29 29 } 30 30 } 31 - function Wibble$Wibble(wobble) { 32 - return new Wibble(wobble); 33 - } 34 - function Wibble$isWibble(value) { 35 - return value instanceof Wibble; 36 - } 37 - function Wibble$Wibble$wobble(value) { 38 - return value.wobble; 39 - } 40 31 41 32 export function main() { 42 33 let tmp = new Wibble("wibble");
-24
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__functions__labelled_argument_ordering.snap
··· 29 29 import { CustomType as $CustomType } from "../gleam.mjs"; 30 30 31 31 class A extends $CustomType {} 32 - function A$A() { 33 - return new A(); 34 - } 35 - function A$isA(value) { 36 - return value instanceof A; 37 - } 38 32 39 33 class B extends $CustomType {} 40 - function B$B() { 41 - return new B(); 42 - } 43 - function B$isB(value) { 44 - return value instanceof B; 45 - } 46 34 47 35 class C extends $CustomType {} 48 - function C$C() { 49 - return new C(); 50 - } 51 - function C$isC(value) { 52 - return value instanceof C; 53 - } 54 36 55 37 class D extends $CustomType {} 56 - function D$D() { 57 - return new D(); 58 - } 59 - function D$isD(value) { 60 - return value instanceof D; 61 - } 62 38 63 39 function wibble(a, b, c, d) { 64 40 return undefined;
-9
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__use___patterns.snap
··· 27 27 this[0] = $0; 28 28 } 29 29 } 30 - function Box$Box($0) { 31 - return new Box($0); 32 - } 33 - function Box$isBox(value) { 34 - return value instanceof Box; 35 - } 36 - function Box$Box$0(value) { 37 - return value[0]; 38 - } 39 30 40 31 function apply(arg, fun) { 41 32 return fun(arg);