Skip to content

Commit 7c5515e

Browse files
committed
fix: declare runes properly in filepath
Signed-off-by: Christian Stewart <christian@aperture.us>
1 parent d0d7e43 commit 7c5515e

9 files changed

Lines changed: 106 additions & 3 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
separator: 47
2+
newline: 10
3+
space: 32
4+
separator matches '/'
5+
newline matches '\n'
6+
space matches ' '
7+
separator + 1: 48
8+
space - 1: 31

compliance/tests/rune_const_import/index.ts

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import "github.com/aperturerobotics/goscript/compliance/tests/rune_const_import/subpkg"
4+
5+
func main() {
6+
// Test importing rune constants from another package
7+
const separator = subpkg.Separator
8+
const newline = subpkg.Newline
9+
const space = subpkg.Space
10+
11+
// Print the imported rune constants
12+
println("separator:", separator)
13+
println("newline:", newline)
14+
println("space:", space)
15+
16+
// Use them in comparisons to ensure they're actually numbers
17+
if separator == '/' {
18+
println("separator matches '/'")
19+
}
20+
if newline == '\n' {
21+
println("newline matches '\\n'")
22+
}
23+
if space == ' ' {
24+
println("space matches ' '")
25+
}
26+
27+
// Test arithmetic operations (only works with numbers)
28+
println("separator + 1:", separator+1)
29+
println("space - 1:", space-1)
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Generated file based on rune_const_import.go
2+
// Updated when compliance tests are re-run, DO NOT EDIT!
3+
4+
import * as $ from "@goscript/builtin/index.js";
5+
6+
import * as subpkg from "@goscript/github.com/aperturerobotics/goscript/compliance/tests/rune_const_import/subpkg/index.js"
7+
8+
export async function main(): Promise<void> {
9+
// Test importing rune constants from another package
10+
let separator: number = subpkg.Separator
11+
let newline: number = subpkg.Newline
12+
let space: number = subpkg.Space
13+
14+
// Print the imported rune constants
15+
console.log("separator:", 47)
16+
console.log("newline:", 10)
17+
console.log("space:", 32)
18+
19+
// Use them in comparisons to ensure they're actually numbers
20+
if (47 == 47) {
21+
console.log("separator matches '/'")
22+
}
23+
if (10 == 10) {
24+
console.log("newline matches '\\n'")
25+
}
26+
if (32 == 32) {
27+
console.log("space matches ' '")
28+
}
29+
30+
// Test arithmetic operations (only works with numbers)
31+
console.log("separator + 1:", 47 + 1)
32+
console.log("space - 1:", 32 - 1)
33+
}
34+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { Newline, Separator, Space } from "./subpkg.gs.js"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package subpkg
2+
3+
// Rune constants that should be compiled as numbers, not strings
4+
const Separator = '/'
5+
const Newline = '\n'
6+
const Space = ' '
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Generated file based on subpkg/subpkg.go
2+
// Updated when compliance tests are re-run, DO NOT EDIT!
3+
4+
import * as $ from "@goscript/builtin/index.js";
5+
6+
export let Separator: number = 47
7+
8+
export let Newline: number = 10
9+
10+
export let Space: number = 32
11+

gs/builtin/slice.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,19 @@ export const stringToRunes = (str: string): number[] => {
10231023
return Array.from(str).map((c) => c.codePointAt(0) || 0)
10241024
}
10251025

1026+
/**
1027+
* Converts a single-character string to its Unicode code point (rune).
1028+
* Used for readable rune constants like $.stringToRune('/') instead of 47.
1029+
* @param str A single-character string.
1030+
* @returns The Unicode code point as a number.
1031+
*/
1032+
export const stringToRune = (str: string): number => {
1033+
if (str.length === 0) {
1034+
return 0
1035+
}
1036+
return str.codePointAt(0) || 0
1037+
}
1038+
10261039
/**
10271040
* Converts an array of Unicode code points (runes) to a string.
10281041
* @param runes The input array of numbers representing Unicode code points.

gs/path/filepath/path.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import * as $ from "@goscript/builtin/index.js"
44

55
// Path separator constants
6-
export const Separator = '/'
7-
export const ListSeparator = ':'
6+
export const Separator = $.stringToRune('/')
7+
export const ListSeparator = $.stringToRune(':')
88

99
// Error constants
1010
export const SkipDir = $.newError('skip this directory')
@@ -234,7 +234,7 @@ export function SplitList(path: string): string[] {
234234
if (path === '') {
235235
return []
236236
}
237-
return path.split(ListSeparator)
237+
return path.split(String.fromCharCode(ListSeparator))
238238
}
239239

240240
// HasPrefix tests whether the path p begins with prefix.

0 commit comments

Comments
 (0)