Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,22 @@ class URL extends Builtin {

class Stream extends Builtin {
constructor(generator) {
const methods = ['readAsBytes', 'readAsJSON', 'readAsString', 'readAsSSE'];
const methods = ['readAsBytes', 'readAsJSON', 'readAsString'];
super(generator, `${CORE}Stream`, methods);
}

readAsSSE(ast, level, async) {
this.generator.imports.push(_getImport(this.module));
this.generator.emit(`${this.module}.read_as_sse`);
if(async) {
this.generator.emit('_async');
}
this.generator.visitArgs(ast.args, level);
}

write(ast, level, async) {
if(async) {
this.emit('awiat ');
this.emit('await ');
}
this.generator.emit(`${_snakeCase(ast.left.id.lexeme)}.write${async ? '_async' : ''}(`);
this.generator.visitExpr(ast.args[0], level);
Expand Down Expand Up @@ -431,7 +440,7 @@ class Func {
sleep(ast, level, async) {
const clientName = this.getClientName();
if(async) {
this.generator.emit('awiat ');
this.generator.emit('await ');
}
this.generator.emit(`${clientName}.sleep`);
if(async) {
Expand Down
25 changes: 16 additions & 9 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1741,17 +1741,19 @@ setup(
assert.equal(ast.left.type, 'method_call');
const name = _name(ast.left.id);
const functionName = _avoidKeywords(_snakeCase(name));
const sse = ast.notes && ast.notes.find(note => note.note.lexeme === '@sse');
const isSSE = sse && sse.arg.value;
if (name.startsWith('$') && this.builtin[name]) {
const method = name.replace('$', '');
this.builtin[name][method](ast, level, ast.isAsync && this.isAsyncFunction);
return;
} else if (this.isStaticFunction === false && this.isAsyncFunction && ast.isAsync) {
this.emit(`await self.${functionName}_async`);
} else if (this.isStaticFunction === false && this.isAsyncFunction && ast.isAsync){
this.emit(`${isSSE ? '' : 'await '}self.${functionName}_async`);
} else if (this.isStaticFunction === false) {
this.emit(`self.${functionName}`);
} else if (ast.isAsync && this.isAsyncFunction) {
this.emit(`await ${this.className}.${functionName}_async`);
} else {
this.emit(`${isSSE ? '' : 'await '}${this.className}.${functionName}_async`);
} else {
const functionName = _snakeCase(name);
this.emit(`${this.className}.${functionName}`);
}
Expand All @@ -1763,7 +1765,9 @@ setup(
assert.equal(ast.left.type, 'instance_call');
const method = _name(ast.left.propertyPath[0]);
const async = ast.isAsync && this.isAsyncFunction;
if (async) {
const sse = ast.notes && ast.notes.find(note => note.note.lexeme === '@sse');
const isSSE = sse && sse.arg.value;
if (async && !isSSE) {
this.emit('await ');
}
if (ast.builtinModule && this.builtin[ast.builtinModule] && this.builtin[ast.builtinModule][method]) {
Expand Down Expand Up @@ -1796,8 +1800,10 @@ setup(

const functionName = _avoidKeywords(_snakeCase(_name(ast.left.propertyPath[0])));

const sse = ast.notes && ast.notes.find(note => note.note.lexeme === '@sse');
const isSSE = sse && sse.arg.value;
if (ast.isAsync && this.isAsyncFunction) {
this.emit(`await ${clientName}.${functionName}_async`);
this.emit(`${isSSE ? '' : 'await '}${clientName}.${functionName}_async`);
} else {
this.emit(`${clientName}.${functionName}`);
}
Expand Down Expand Up @@ -2465,7 +2471,8 @@ setup(
// if (ast.annotation) {
// this.emit(`${_anno(ast.annotation.value)}\n`, level);
// }

const sse = ast.notes.find(note => note.note.lexeme === '@sse');
const isSSE = sse && sse.arg.value;
this.visitAnnotation(ast.annotation, level);
let comments = DSL.comment.getFrontComments(this.comments, ast.tokenRange[0]);
this.visitComments(comments, level);
Expand All @@ -2488,7 +2495,7 @@ setup(
this.visitAPIBody(ast.apiBody, secondLevel);
}
this.emit(`_last_request = ${REQUEST}\n`, secondLevel);
this.emit(`${RESPONSE} = ${CORE}Core.do_action(${REQUEST}`, secondLevel);
this.emit(`${RESPONSE} = ${CORE}Core.${isSSE ? 'do_sse_action' : 'do_action'}(${REQUEST}`, secondLevel);
if (ast.runtimeBody) {
this.emit(', _runtime');
}
Expand Down Expand Up @@ -2521,7 +2528,7 @@ setup(
// temp level
this.visitAPIBody(ast.apiBody, secondLevel);
this.emit(`_last_request = ${REQUEST}\n`, secondLevel);
this.emit(`${RESPONSE} = await ${CORE}Core.async_do_action(${REQUEST}`, secondLevel);
this.emit(`${RESPONSE} = await ${CORE}Core.${isSSE ? 'async_do_sse_action' : 'async_do_action'}(${REQUEST}`, secondLevel);
if (ast.runtimeBody) {
this.emit(', _runtime');
}
Expand Down
7 changes: 7 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const builtinModels = [
'$ResponseError', '$FileField'
];

const words = ['SSE'];

const CORE = 'Dara';
const REQUEST = '_request';
const RESPONSE = '_response';
Expand Down Expand Up @@ -219,6 +221,11 @@ function _snakeCase(str) {
}
let res = '';
let tmp = '';
words.forEach(word => {
// 只匹配驼峰词前缀,不破坏开头
const regex = new RegExp(word + '(?=[A-Z0-9])', 'g');
str = str.replace(regex, _upperFirst(word.toLowerCase()));
});
for (const c of str) {
if (/[A-Z|0-9]/.test(c)) {
tmp += c;
Expand Down
2 changes: 1 addition & 1 deletion tests/expected/builtin/tea_python_tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ async def main_async(
if not DaraCore.is_null(data):
ws.write(data)

awiat DaraCore.sleep_async(a)
await DaraCore.sleep_async(a)
default_val = args[0] or args[1]
if default_val == b:
return
Expand Down
8 changes: 4 additions & 4 deletions tests/expected/complex/tea_python_tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def complex_1(
return
elif SourceClient.judge_str('test') or False:
return source_models.RuntimeObject()
source_client.print_data(DaraCore.to_map(request), '1')
source_client.print_data_async(DaraCore.to_map(request), '1')
self.hello(DaraCore.to_map(request), [
'1',
'2'
Expand Down Expand Up @@ -442,7 +442,7 @@ async def complex_1_async(
return
elif SourceClient.judge_str('test') or False:
return source_models.RuntimeObject()
source_client.print_data(DaraCore.to_map(request), '1')
await source_client.print_data_async_async(DaraCore.to_map(request), '1')
await self.hello_async(DaraCore.to_map(request), [
'1',
'2'
Expand Down Expand Up @@ -477,7 +477,7 @@ def complex_2(
config_array = [
config
]
source_client.print_data(DaraCore.to_map(request), '1')
source_client.print_data_sse(DaraCore.to_map(request), '1')
_request.protocol = 'HTTP'
_request.port = 80
_request.method = 'GET'
Expand All @@ -504,7 +504,7 @@ async def complex_2_async(
config_array = [
config
]
source_client.print_data(DaraCore.to_map(request), '1')
source_client.print_data_sse_async(DaraCore.to_map(request), '1')
_request.protocol = 'HTTP'
_request.port = 80
_request.method = 'GET'
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/complex/libraries/import.dara
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ type @pathname = string

init(config: Config);
model Config = {}

function printData(runtime: object, str: string): void {
return;
}

async function printDataAsync(runtime: object, str: string): void {
return;
}

@sse(true)
async function printDataSSE(runtime: object, str: string): void {
return;
}


static function judgeStr(a: string): boolean {
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/complex/main.dara
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ api Complex1(request: ComplexRequest, sourceClient: Source): Source.RuntimeObjec
} else if(Source.judgeStr("test") || false){
return new Source.RuntimeObject{};
}
sourceClient.printData(request, '1');
sourceClient.printDataAsync(request, '1');
hello(request, ["1","2"], null);
hello(null, null, null);
return {};
Expand All @@ -262,7 +262,7 @@ api Complex2(request: ComplexRequest, str: [ string ], val: map[string]string):
var config = new Source.Config{};
var sourceClient = new Source(config);
var configArray : [Source.Config] = [config];
sourceClient.printData(request, '1');
sourceClient.printDataSSE(request, '1');
__request.protocol = 'HTTP';
__request.port = 80;
__request.method = 'GET';
Expand Down