This repository has been archived by the owner on May 15, 2023. It is now read-only.
/
host_callable.dart
64 lines (56 loc) · 2.14 KB
/
host_callable.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2019 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
// ignore: deprecated_member_use
import 'dart:cli';
import 'dart:io';
import 'package:sass_api/sass_api.dart' as sass;
import 'dispatcher.dart';
import 'embedded_sass.pb.dart';
import 'function_registry.dart';
import 'protofier.dart';
import 'utils.dart';
/// Returns a Sass callable that invokes a function defined on the host with the
/// given [signature].
///
/// If [id] is passed, the function will be called by ID (which is necessary for
/// anonymous functions defined on the host). Otherwise, it will be called using
/// the name defined in the [signature].
///
/// Throws a [sass.SassException] if [signature] is invalid.
sass.Callable hostCallable(Dispatcher dispatcher, FunctionRegistry functions,
int compilationId, String signature,
{int? id}) {
sass.Callable? callable;
callable = sass.Callable.host(signature, (arguments) {
var protofier = Protofier(dispatcher, functions, compilationId);
var request = OutboundMessage_FunctionCallRequest()
..compilationId = compilationId
..arguments.addAll(
[for (var argument in arguments) protofier.protofy(argument)]);
if (id != null) {
request.functionId = id;
} else {
request.name = callable!.name;
}
// ignore: deprecated_member_use
var response = waitFor(dispatcher.sendFunctionCallRequest(request));
try {
switch (response.whichResult()) {
case InboundMessage_FunctionCallResponse_Result.success:
return protofier.deprotofyResponse(response);
case InboundMessage_FunctionCallResponse_Result.error:
throw response.error;
case InboundMessage_FunctionCallResponse_Result.notSet:
throw mandatoryError('FunctionCallResponse.result');
}
} on ProtocolError catch (error) {
error.id = errorId;
stderr.writeln("Host caused ${error.type.name.toLowerCase()} error: "
"${error.message}");
dispatcher.sendError(error);
throw error.message;
}
});
return callable;
}