Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous fixes #97

Merged
merged 7 commits into from
Aug 15, 2019
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
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
build/
.dart_tool/
deploy/
# Remove the following pattern if you wish to check in your lock file
pubspec.lock

# Directory created by dartdoc
doc/api/
Expand Down
3 changes: 2 additions & 1 deletion lib/src/core/logging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class SchemeException extends Value implements Exception {
if (!showTrace || callStack.isEmpty) return 'Error: $message';
var str = 'Traceback (most recent call last)\n';
for (int i = 0; i < callStack.length; i++) {
str += '$i\t${callStack[i]}\n';
var space = " " * (6 - i.toString().length);
str += '$i$space${callStack[i]}\n';
}
return str + 'Error: $message';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/special_forms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Undefined doSetForm(SchemeList<Expression> expressions, Frame env) {
checkForm(expressions, 2, 2);
Expression name = expressions.first;
if (name is! SchemeSymbol) throw SchemeException("$name is not a symbol");
Expression value = schemeEval(expressions.skip(1).first, env);
Value value = schemeEval(expressions.skip(1).first, env);
env.update(name as SchemeSymbol, value);
return undefined;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/src/core/values.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ class Promise extends Value {
/// Evaluates the promise, or returns the result if already evaluated.
Expression force() {
if (!_evaluated) {
expr = schemeEval(expr, env);
env.interpreter.language.validateCdr(expr,
expr = env.interpreter.language.validateCdr(schemeEval(expr, env),
errorMessage: "A promise must contain a pair, stream, or nil");
_evaluated = true;
}
Expand Down
48 changes: 28 additions & 20 deletions lib/src/web/js_interop.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
@JS()
library cs61a_scheme.web.js_interop;

import 'dart:async';
import 'dart:js';

import 'package:js/js.dart';

import 'package:cs61a_scheme/cs61a_scheme_extra.dart';

class JsProcedure extends Procedure {
final SchemeSymbol name = null;
final JsFunction fn;
JsProcedure(this.fn);
Expression apply(SchemeList args, Frame env) {
Value apply(SchemeList args, Frame env) {
var result = fn.apply(args.map((arg) => arg.toJS()).toList());
return jsToScheme(result);
}

toString() => "#[js-function]";

toJS() => fn;
}

Expand Down Expand Up @@ -42,19 +47,21 @@ class NativeValue extends Value {
}

Value jsToScheme(obj) {
if (obj is Expression) return obj;
if (obj is Value) return obj;
if (obj is num) return Number.fromNum(obj);
if (obj is bool) return obj ? schemeTrue : schemeFalse;
if (obj is String) return SchemeString(obj);
if (obj is SchemeFunction) return obj.procedure;
if (obj is JsFunction) return JsProcedure(obj);
if (obj is JsObject) return jsObjectToScheme(obj);
if (obj == null) return undefined;
if (obj == context['undefined']) return undefined;
return NativeValue(obj);
return unwrapSchemeProcedure(obj) ?? NativeValue(obj);
}

Value jsObjectToScheme(JsObject obj) {
if (obj.hasProperty('wrappedSchemeProcedure')) {
return obj['wrappedSchemeProcedure'];
}
if (obj is JsFunction) return JsProcedure(obj);
var type =
context['Object']['prototype']['toString'].callMethod('call', [obj]);
if (type == '[object Promise]') {
Expand All @@ -66,7 +73,7 @@ Value jsObjectToScheme(JsObject obj) {
return JsValue(obj);
}

Expression jsEval(String code) {
Value jsEval(String code) {
try {
return jsToScheme(context.callMethod("eval", [code]));
// ignore: avoid_catches_without_on_clauses
Expand All @@ -76,17 +83,18 @@ Expression jsEval(String code) {
}
}

class SchemeFunction {
Procedure procedure;
Frame env;
SchemeFunction(this.procedure, this.env);
call() => schemeApply(procedure, SchemeList(nil), null).toJS();
noSuchMethod(Invocation invocation) {
if (invocation.memberName == const Symbol("call")) {
var args = invocation.positionalArguments.map(jsToScheme);
return schemeApply(procedure, SchemeList.fromIterable(args), env).toJS();
}
throw SchemeException(
"Something has gone horribly wrong with wrapped procedures");
}
}
@JS("wrapSchemeProcedure")
external JsFunction wrapSchemeProcedure(dynamic fn(args), procedure);

@JS("unwrapSchemeProcedure")
external Procedure unwrapSchemeProcedure(obj);

JsFunction procedureToJsFunction(Procedure procedure, Frame env) =>
wrapSchemeProcedure(allowInterop((args) {
var schemeArgs = <Value>[];
for (var arg in args) {
schemeArgs.add(jsToScheme(arg));
}
return schemeApply(procedure, SchemeList.fromIterable(schemeArgs), env)
.toJS();
}), procedure);
3 changes: 2 additions & 1 deletion lib/src/web/web_library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class WebLibrary extends SchemeLibrary with _$WebLibraryMixin {

void importAll(Frame env) {
super.importAll(env);
Procedure.jsProcedure = (procedure) => SchemeFunction(procedure, env);
Procedure.jsProcedure =
(procedure) => procedureToJsFunction(procedure, env);
}

/// Evaluates a piece of JavaScript code and returns the result.
Expand Down
Loading