Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit d6e8ac4

Browse files
authored
coeffects and capabilities (#142)
* feat: add contexts to the methods/funtions * chore: empty string patch * chore: test the coeffects functions and change coeffects property to keyset from string
1 parent 50023d3 commit d6e8ac4

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Hack Codegen is a library for programmatically generating Hack code",
44
"keywords": ["code generation", "Hack"],
55
"require": {
6-
"hhvm": "^4.80",
6+
"hhvm": "^4.93",
77
"hhvm/hhvm-autoload": "^2.0|^3.0",
88
"hhvm/hsl": "^4.0"
99
},

src/CodegenFunctionish.hack

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ abstract class CodegenFunctionish implements ICodeBuilderRenderer {
2222
protected string $name;
2323
protected ?string $body = null;
2424
protected ?string $docBlock = null;
25+
protected ?keyset<string> $contexts = null;
2526
protected ?string $returnType = null;
2627
private ?string $fixme = null;
2728
protected bool $isAsync = false;
@@ -53,6 +54,28 @@ abstract class CodegenFunctionish implements ICodeBuilderRenderer {
5354
return $this;
5455
}
5556

57+
public function addContext(
58+
string $context,
59+
): this {
60+
if($this->contexts === null) {
61+
$this->contexts = keyset<string>[$context];
62+
} else {
63+
$this->contexts[] = $context;
64+
}
65+
66+
return $this;
67+
}
68+
69+
public function addContexts(
70+
Traversable<string> $contexts,
71+
): this {
72+
foreach($contexts as $context) {
73+
$this->addContext($context);
74+
}
75+
76+
return $this;
77+
}
78+
5679
public function setReturnType(string $type): this {
5780
return $this->setReturnTypef('%s', $type);
5881
}
@@ -129,6 +152,10 @@ abstract class CodegenFunctionish implements ICodeBuilderRenderer {
129152
return $this->parameters;
130153
}
131154

155+
public function getContexts(): ?keyset<string> {
156+
return $this->contexts;
157+
}
158+
132159
public function getReturnType(): ?string {
133160
return $this->returnType;
134161
}
@@ -151,6 +178,7 @@ abstract class CodegenFunctionish implements ICodeBuilderRenderer {
151178
$builder = (new HackBuilder($this->config))
152179
->add($keywords)
153180
->addf('%s(%s)', $this->name, Str\join($this->parameters, ', '))
181+
->addIf($this->contexts !== null, '[' . Str\join($this->contexts ?? keyset[], ', ') . ']')
154182
->addIf($this->returnType !== null, ': '.($this->returnType ?? ''));
155183

156184
$code = $builder->getCode();
@@ -183,6 +211,7 @@ abstract class CodegenFunctionish implements ICodeBuilderRenderer {
183211
->addLines($parameter_lines)
184212
->unindent()
185213
->add(')')
214+
->addIf($this->contexts !== null, '[' . Str\join($this->contexts ?? keyset[], ', ') . ']')
186215
->addIf($this->returnType !== null, ': '.($this->returnType ?? ''));
187216

188217
return $multi_line_builder->getCode();

tests/CodegenFunctionTest.codegen

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public function getExpensiveNotLikeParent() {
6161
public async function genExpensiveNotLikeParent() {
6262
}
6363

64+
!@#$%codegentest:testContexts
65+
function withContexts()[io]: void {
66+
echo 'hello world';
67+
}
68+
6469
!@#$%codegentest:testParams
6570
function getName(string $name) {
6671
return $name . $name;

tests/CodegenFunctionTest.hack

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ final class CodegenFunctionTest extends CodegenBaseTest {
2424
expect_with_context(static::class, $code)->toBeUnchanged();
2525
}
2626

27+
public function testContexts(): void {
28+
$code = $this
29+
->getCodegenFactory()
30+
->codegenFunction('withContexts')
31+
->addContext('io')
32+
->setReturnType('void')
33+
->setBody('echo \'hello world\';')
34+
->render();
35+
expect_with_context(static::class, $code)->toBeUnchanged();
36+
}
37+
2738
public function testParams(): void {
2839
$code = $this
2940
->getCodegenFactory()

0 commit comments

Comments
 (0)