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

【feature讨论】沙箱内外部对象的传递 #7

Open
IAIAE opened this issue Jun 30, 2020 · 1 comment
Open

【feature讨论】沙箱内外部对象的传递 #7

IAIAE opened this issue Jun 30, 2020 · 1 comment

Comments

@IAIAE
Copy link
Owner

IAIAE commented Jun 30, 2020

问题背景

沙箱为了能够在es5的环境下提供更加高级的语法支持,将许多原生的构造类进行了封装。例如Array类型,无论是new Array还是字面量形式let t = [1,2,3],在沙箱中的实现都是绑定了一个构造类:MArray。所以,如果将一个沙箱内部的数组对象传给外部,是无法当做数组来使用的,arr.entries()方法返回的也是一个模拟的MIterable。另外,外面的对象传入沙箱内部,怎么办?需要转换么?将原生的数组转换为MArray,将Iterable转换为MIaterable

举一个例子,如果沙箱内部执行后返回个React.Component。在外部使用这个组件,什么情况会产生问题呢?<SomeComp />实际是调用React.createElement(SomeComponent),应该不会产生问题;任何对组件的操作,方法都是沙箱内部实现的,不用和外部有何通信,所以不会产生问题。

如果沙箱向外部返回一个方法,该方法执行返回一个数组,那么就会出现问题。

怎么解决这类问题呢?

@IAIAE
Copy link
Owner Author

IAIAE commented Jul 1, 2020

目前来说,是尽量减小内外部对象实现的差异性。Array类型,内外部通用的接口已经做到了兼容。但外部调用内部数组的.entries()方法得到还是内部模拟的MIterable,开发者在使用的时候要避免这种情况。本来在内部生成的非纯数据类型的对象,都不应该交给外部使用。内外部通信传递的,应该是完全封装好的类(类方法的实现全部内聚,如果向外部返回的话,也是返回数据对象),或者纯数据对象。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant