Skip to content

Instantly share code, notes, and snippets.

@vadimtsushko
Last active February 23, 2016 16:22
Show Gist options
  • Save vadimtsushko/06f238dc9c364f403dfa to your computer and use it in GitHub Desktop.
Save vadimtsushko/06f238dc9c364f403dfa to your computer and use it in GitHub Desktop.
How to read properties from opaque dynamic javascript map
<!DOCTYPE html>
<!--
Copyright (c) 2016, <your name>. All rights reserved. Use of this source code
is governed by a BSD-style license that can be found in the LICENSE file.
-->
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="scaffolded-by" content="https://github.com/google/stagehand">
<title>test_interop</title>
<link rel="stylesheet" href="styles.css">
<script type="text/javascript">
function mapFromJs() {
return {message: 'HELLO FROM JS', level: 'INFO'};
}
</script>
<script defer src="main.dart" type="application/dart"></script>
<script defer src="packages/browser/dart.js"></script>
</head>
<body>
<div id="output"></div>
</body>
</html>
// Copyright (c) 2016, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.
import 'dart:html';
import "package:js/js.dart";
void main() {
var v = mapFromJs();
var message = getValue(v,'message');
print('message $message');
querySelector('#output').text = message.toString();
}
@JS()
external mapFromJs();
@JS()
@anonymous
class Description {
external get value;
external factory Description({bool configurable, bool enumerable, value});
}
@JS('Object.defineProperty')
external void defineProperty(o, String prop, Description description);
setValue(o, String key, value) =>
defineProperty(o, key, new Description(value: value));
@JS('Object.getOwnPropertyDescriptor')
external Description getOwnPropertyDescriptor(o, String prop);
getValue(o, String key)=> getOwnPropertyDescriptor(o,key)?.value;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment