Skip to content

Commit 8b8e352

Browse files
committed
fix(claude): add utility methods to v8 generator
1 parent 35cec95 commit 8b8e352

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

pkg/generator/v8.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ func (g *V8Generator) generateClasses(m *manifest.Manifest) (string, error) {
372372
func (g *V8Generator) generateClass(m *manifest.Manifest, class *manifest.Class) (string, error) {
373373
var sb strings.Builder
374374

375+
hasDtor := class.Destructor != ""
376+
375377
// Class JSDoc comment
376378
if class.Description != "" {
377379
sb.WriteString(fmt.Sprintf(" /** %s */\n", class.Description))
@@ -394,6 +396,9 @@ func (g *V8Generator) generateClass(m *manifest.Manifest, class *manifest.Class)
394396
sb.WriteString(" constructor();\n\n")
395397
}
396398

399+
// Generate utility methods (valid, get, release, and close if destructor exists)
400+
sb.WriteString(g.generateUtilityMethods(class, hasDtor))
401+
397402
// Generate bindings (methods)
398403
for _, binding := range class.Bindings {
399404
methodCode, err := g.generateBinding(m, class, &binding)
@@ -408,6 +413,44 @@ func (g *V8Generator) generateClass(m *manifest.Manifest, class *manifest.Class)
408413
return sb.String(), nil
409414
}
410415

416+
func (g *V8Generator) generateUtilityMethods(class *manifest.Class, hasDtor bool) string {
417+
var sb strings.Builder
418+
419+
// Get the handle type mapped to TypeScript
420+
handleType, _ := g.typeMapper.MapType(class.HandleType, TypeContextReturn, false)
421+
422+
// valid() method
423+
sb.WriteString(" /**\n")
424+
sb.WriteString(" * Check if the handle is valid.\n")
425+
sb.WriteString(" * @returns True if the handle is valid, false otherwise\n")
426+
sb.WriteString(" */\n")
427+
sb.WriteString(" valid(): boolean;\n\n")
428+
429+
// get() method
430+
sb.WriteString(" /**\n")
431+
sb.WriteString(" * Get the raw handle value without transferring ownership.\n")
432+
sb.WriteString(fmt.Sprintf(" * @returns The underlying handle value\n"))
433+
sb.WriteString(" */\n")
434+
sb.WriteString(fmt.Sprintf(" get(): %s;\n\n", handleType))
435+
436+
// release() method
437+
sb.WriteString(" /**\n")
438+
sb.WriteString(" * Release ownership of the handle and return it.\n")
439+
sb.WriteString(fmt.Sprintf(" * @returns The released handle value\n"))
440+
sb.WriteString(" */\n")
441+
sb.WriteString(fmt.Sprintf(" release(): %s;\n\n", handleType))
442+
443+
// close() method - only if destructor exists
444+
if hasDtor {
445+
sb.WriteString(" /**\n")
446+
sb.WriteString(" * Close and destroy the handle if owned.\n")
447+
sb.WriteString(" */\n")
448+
sb.WriteString(" close(): void;\n\n")
449+
}
450+
451+
return sb.String()
452+
}
453+
411454
func (g *V8Generator) generateConstructor(m *manifest.Manifest, class *manifest.Class, methodName string) (string, error) {
412455
// Find the method in the manifest
413456
var method *manifest.Method

0 commit comments

Comments
 (0)