Skip to content

Commit

Permalink
lem-server: change js-eval to return the result of evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
cxxxr committed Oct 22, 2024
1 parent 9da6793 commit 9fd0c46
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion frontends/server/frontend/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}

</style>
<script type="module" crossorigin src="/assets/index-DkE9_tVc.js"></script>
<script type="module" crossorigin src="/assets/index-B5dt4B6H.js"></script>
</head>
<body>
<div id='lem-editor'></div>
Expand Down
12 changes: 8 additions & 4 deletions frontends/server/frontend/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class BaseSurface {
}

evalIn(code) {
eval(code);
return eval(code);
}
}

Expand Down Expand Up @@ -438,7 +438,7 @@ class HTMLSurface extends BaseSurface {
}

evalIn(code) {
this.iframe.contentWindow.eval(code);
return this.iframe.contentWindow.eval(code);
}
}

Expand Down Expand Up @@ -707,7 +707,7 @@ class View {
}

evalIn(code) {
this.mainSurface.evalIn(code);
return this.mainSurface.evalIn(code);
}
}

Expand Down Expand Up @@ -1205,6 +1205,10 @@ export class Editor {

jsEval({ viewInfo: { id }, code }) {
const view = this.findViewById(id);
view.evalIn(code);
const result = view.evalIn(code);
if (result) {
return result.toString();
}
return result;
}
}
33 changes: 28 additions & 5 deletions frontends/server/main.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,34 @@
;; TODO
1)

(defmethod lem-if:js-eval ((jsonrpc jsonrpc) view code)
(notify (lem:implementation)
"js-eval"
(hash "viewInfo" view
"code" code)))
(defmethod lem-if:js-eval ((jsonrpc jsonrpc) view code &key wait)
(let ((params (hash "viewInfo" view "code" code)))
(if wait
(let ((mailbox (sb-concurrency:make-mailbox :name "js-eval-mailbox")))
(apply #'values
(loop :for connection
:in (jsonrpc/server::server-client-connections
(jsonrpc-server (lem:implementation)))
:do (jsonrpc:call-async-to
(jsonrpc-server (lem:implementation))
connection
"js-eval"
params
(lambda (res)
(sb-concurrency:send-message mailbox (list t res)))
(lambda (message code)
(sb-concurrency:send-message
mailbox
(list nil
(make-condition 'jsonrpc/errors:jsonrpc-callback-error
:message message
:code code)))))))
(destructuring-bind (ok value)
(sb-concurrency:receive-message mailbox)
(if ok
value
(error value))))
(notify (lem:implementation) "js-eval" params))))

(lem:add-hook lem:*switch-to-buffer-hook* 'on-switch-to-buffer)

Expand Down
5 changes: 3 additions & 2 deletions src/html-buffer.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
(change-class buffer 'html-buffer :html html)
buffer))

(defun js-eval (window code)
(defun js-eval (window code &key (wait nil))
(lem-if:js-eval (implementation)
(window-view window)
code))
code
:wait wait))
7 changes: 4 additions & 3 deletions src/interface.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@
(defgeneric lem-if:object-height (implementation drawing-object))
(defgeneric lem-if:clear-to-end-of-window (implementation view y))

(defgeneric lem-if:js-eval (implementation view code)
(:method (implementation view code)
(error "unimplemented")))
(defgeneric lem-if:js-eval (implementation view code &key wait)
(:method (implementation view code &key wait)
(declare (ignore wait))
(error "unimplemented")))

(defvar *display-background-mode* nil)

Expand Down

0 comments on commit 9fd0c46

Please sign in to comment.